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

发布于 2024-11-27 21:27:56 字数 403 浏览 5 评论 0原文

以下代码部分中标记为“版本 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);
}

据我了解,“版本 1”被视为函数声明。但我不明白为什么,也不明白返回类型为 vector 的结果函数 v 的参数是什么。

What is the difference between the code snippets labeled "version 1" and "version 2" in the following code section:

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. But I don't understand why nor what the arguments of the resulting function v with return type vector<int> are.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹倦 2024-12-04 21:27:56

为什么

因为标准或多或少地说,任何可能被解释为函数声明的东西都将在任何上下文中,无论如何。

论据是什么......

你可能不相信这一点,但这是真的。 input(cin) 被视为输入 cin;在这个地方,括号是允许的,但毫无意义。但是,input() 不会被视为声明没有名称的 input 类型的参数;相反,它是一个input(*)()类型的参数,即指向不带参数并返回input的函数的指针。显然,(*) 部分在声明类型时是不必要的。我猜出于同样的原因,当您使用函数名称来初始化函数指针时, & 是可选的......

另一种解决此问题的方法是利用我们声明的事实无论如何,单独的值来证明跳过 typedef 是合理的:

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

另一种方法是以函数声明不允许的方式添加括号:

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

有关更多信息,Google“c++ 最令人烦恼的解析”。

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; in this spot, parentheses are allowed and simply meaningless. However, input() is not treated as declaring a parameter of type input with no name; instead, it is a parameter of type input(*)(), i.e. a pointer to a function taking no arguments and returning an 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:

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".

旧城空念 2024-12-04 21:27:56

这被称为最令人烦恼的解析

这个片段:

  input()

可以作为

  1. 变量类的变量定义 来消除歧义输入,采用类输入的匿名实例或
  2. 函数的函数声明,该函数返回输入类型的对象并采用单个(未命名)参数,该参数是返回类型输入(并且不接受输入)的函数。

大多数程序员期望第一个,但 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.

要走就滚别墨迹 2024-12-04 21:27:56
vector<int> v(input(cin), input());

好吧,这个函数声明的参数如下:

  • input(cin) - 这是一个对象
  • input (*)() - 这是一个指向返回函数的指针 >输入并且不接受任何参数。
vector<int> v(input(cin), input());

Well, the arguments to this function declaration are these:

  • input(cin) - which is an object
  • input (*)() - which is a pointer to function returning input and taking no argument.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文