|C++ and the gcc/g++ Compiler|
As I discuss on page 146 of my book The C++ Standard Library, you can initialize STL containers with elements from standard input:
However, 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
In this case, c declares a function with a return type that is deque<int>. Its first parameter is of type istream_iterator<int> with the name cin, and its second unnamed parameter is of type "function taking no arguments returning istream_iterator<int>." 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 (thanks to John H. Spicer from EDG for this explanation).
Unfortunately, the some versions
of GCC/G++ (such as 3.3) do NOT allow this kind of initialization and report
a syntax error.
As a workaround, put the parentheses only around the first argument. Then everything is fine.
I updated my code examples for my library book accordingly.