简体   繁体   English

C ++嵌套构造函数调用与函数声明

[英]C++ nested constructor calls vs. function declaration

What is the difference between the code snippets labeled "version 1" and "version 2" in the following code section: 以下代码部分中标记为“版本1”和“版本2”的代码段之间有什么区别:

int main() {
  using namespace std;
  typedef istream_iterator<int> input;

  // version 1)
  //vector<int> v(input(cin), input());

  // version 2)
  input endofinput;
  vector<int> v(input(cin), endofinput);
}

As far as I understand "version 1" is treated as function declaration. 据我了解,“版本1”被视为函数声明。 But I don't understand why nor what the arguments of the resulting function v with return type vector<int> are. 但是我不明白为什么返回类型为vector<int>的结果函数v的参数也不是。

why 为什么

Because the Standard says, more or less, that anything that can possibly be interpreted as a function declaration will be, in any context, no matter what. 因为该标准或多或少地表明,在任何情况下,任何可能被解释为函数声明的东西都将是无论如何。

what the arguments... are 有什么争论...

You might not believe this, but it's true. 您可能不相信这一点,但这是事实。 input(cin) is treated as input cin ; input(cin)视为input cin in this spot, parentheses are allowed and simply meaningless. 在这一点上,括号是允许的,只是没有意义。 However, input() is not treated as declaring a parameter of type input with no name; 但是, input()不能被视为声明类型为input的参数而没有名称; instead, it is a parameter of type input(*)() , ie a pointer to a function taking no arguments and returning an input . 相反,它是类型为input(*)()的参数,即,指向不带参数并返回input的函数的指针。 The (*) part is unnecessary in declaring the type, apparently. 显然,在声明类型时不需要(*)部分。 I guess for the same reason that the & is optional when you use a function name to initialize the function pointer... 我猜出于同样的原因,当您使用函数名称初始化函数指针时, &是可选的...

Another way to get around this, taking advantage of the fact that we're declaring the values separately anyway to justify skipping the typedef: 解决这个问题的另一种方法,利用我们无论如何都要分别声明值来证明跳过typedef的事实:

istream_iterator<int> start(cin), end;
vector<int> v(start, end);

Another way is to add parentheses in a way that isn't allowed for function declarations: 另一种方法是以函数声明所不允许的方式添加括号:

vector<int> v((input(cin)), input());

For more information, Google "c++ most vexing parse". 有关更多信息,请使用Google“最烦人的c ++解析”。

This is called most vexing parse : 这称为最烦人的解析

This snippet : 此代码段:

  input()

could be disambiguated either as 可以消除歧义,因为

  1. a variable definition for variable class input, taking an anonymous instance of class input or 变量类输入的变量定义,采用类输入的匿名实例或
  2. a function declaration for a function which returns an object of type input and takes a single (unnamed) argument which is a function returning type input (and taking no input). 函数的函数声明,该函数返回输入类型的对象并接受单个(未命名)参数,该参数是返回类型输入(不输入)的函数。

Most programmers expect the first, but the C++ standard requires it to be interpreted as the second. 大多数程序员期望第一个,但是C ++标准要求将其解释为第二个。

vector<int> v(input(cin), input());

Well, the arguments to this function declaration are these: 好了,此函数声明的参数如下:

  • input(cin) - which is an object input(cin) -这是一个对象
  • input (*)() - which is a pointer to function returning input and taking no argument. input (*)() -这是指向返回input且不带参数的函数的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM