The Perplexing Case of Temporary Iterators in the Most Vexing Parse
The Most Vexing Parse, a notorious parsing ambiguity in C , poses a perplexing question: how can the following line be interpreted as a function declaration?
vector<int> v(istream_iterator<int>(cin), istream_iterator<int>());
To comprehend this peculiar behavior, we must delve into the details of the Most Vexing Parse. The second temporary iterator can indeed be construed as a type, namely a function that yields an iterator with no parameters. However, the crux lies in understanding the interpretation of the first temporary iterator.
Unlike the second iterator, the first temporary iterator appears confusingly wrapped within parentheses and lacks an explicit parameter name. This unconventional syntax, a legacy of C, instigates the query: what type does this iterator represent?
The answer is that istream_iterator
Ultimately, the perplexing nature of this code stems from its reliance on this obsolete syntax. It declares that the first parameter is an istream_iterator
The above is the detailed content of Why Does `vector v(istream_iterator(cin), istream_iterator());` Declare a Function in C ?. For more information, please follow other related articles on the PHP Chinese website!