call to constructor can be considered as function declaration?

橙三吉。 提交于 2019-12-23 12:28:30

问题


The next few lines I'm going to write come from the book "The C++ Standard Library - A tutorial and reference".

Initialize by using standard input:

//read all integer elements of the deque from standard input
std::deque<int> c((std::istream_iterator<int>(std::cin)),
(std::istream_iterator<int>()));

Don't forget the extra parentheses around the initializer arguments here. Otherwise, this expression does something very different and you probably will get some strange warnings or errors in following statements. Consider writing the statement without extra parentheses:

std::deque<int> c(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>());

In this case, c declares a function with a return type that is deque. Its first parameter is of type istream_iterator with the name cin, and its second unnamed parameter is of type "function taking no arguments returning istream_iterator." This construct is valid syntactically as either a declaration or an expression. So, according to language rules, it is treated as a declaration. The extra parentheses force the initializer not to match the syntax of a declaration.

I can see why the one with extra parentheses is not considered a function declaration, but I can't see what would make the one without into a function declaration though? For it has parentheses around (std::cin), and as far as I know variables may not have names with parentheses?

Am I missing something?


回答1:


The tutorial you read is wrong. This:

std::deque<int> c(std::istream_iterator<int>(std::cin), std::istream_iterator<int>());

Can't be parsed as a function declaration because std::cin can't be a name of a parameter. If you remove the std qualifier though:

std::deque<int> c(std::istream_iterator<int>(cin), std::istream_iterator<int>());

then you get a function declaration.

[...] and as far as I know variables may not have names with parentheses?

The parentheses aren't part of the name. You can just put them there, how many you like:

int ((((((a)))))) = 12345; // valid code!
a++; // the variable is named 'a'



回答2:


If a statement can be parsed as either a function prototype or a variable declaration with a constructor argument, the function prototype takes precedence. This is opposite of most people's expectations, and thus it is named the Most Vexing Parse.




回答3:


as far as I know variables may not have names with parentheses

That's the assumption that's wrong; the parentheses aren't part of the variable name, they're just ignored (in this context).

From the C++ standard ([dcl.decl]):

declarator:

  • ptr-declarator
  • noptr-declarator parameters-and-qualifiers trailing-return-type

ptr-declarator:

  • noptr-declarator
  • ptr-operator ptr-declarator

noptr-declarator:

  • declarator-id attribute-specifieropt
  • noptr-declarator parameters-and-qualifiers
  • noptr-declarator [ constant-expressionopt] attribute-specifieropt
  • ( ptr-declarator )

and ([dcl.fct]):

parameter-declaration-clause:

  • parameter-declaration-listopt...opt
  • parameter-declaration-list , ...

parameter-declaration-list:

  • parameter-declaration
  • parameter-declaration-list , parameter-declaration

parameter-declaration:

  • attribute-specifieropt decl-specifier-seq declarator
  • attribute-specifieropt decl-specifier-seq declarator = assignment-expression
  • attribute-specifieropt decl-specifier-seq abstract-declaratoropt
  • attribute-specifieropt decl-specifier-seq abstract-declaratoropt= assignment-expression

Hopefully you can follow these grammar definitions to see that parentheses are indeed allowed, but if not, just ask and I'll try to clarify.



来源:https://stackoverflow.com/questions/8903082/call-to-constructor-can-be-considered-as-function-declaration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!