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?
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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With