Strange initialization
Written by Antoni Boucher   
Article Index
Strange initialization
Solution

Solution

You add some code to the program in an effort find out what is happening but the result is only that you get very confused.

If you add this line in the main() function, you get a compiler error:

std::cout << data.at(0) << std::endl;

The compiler error message gives you a hint:

request for member ‘at’ in ‘data’, 
which is of non-class type
‘std::vector<std::basic_string<char> >
(std::istream_iterator<std::basic_string<char> >,
std::istream_iterator<std::basic_string<char> >
 (*)())’

The cause of the problem is that the compiler thinks it is facing a function prototype!

However, you want to create a vector, which is a variable definition. Clearly you are not interpreting the syntax in the same way.

How does the compiler see a function prototype?

Look again at this line:

vector<string> data(
istream_iterator<string>(cin),
istream_iterator<string>());

First of all, we have the return type (vector<string>).
After, we see the function name (data).

The rest is more difficult to see.

istream_iterator<string>(cin)

is seen as

istream_iterator<string> cin

by the compiler.  The compiler saw a parameter because the parentheses are superfluous.

Nevertheless, how does the compiler see the last argument?

It sees it as a pointer to a function which does not take any argument and returns istream_iterator<string>.

So the line can be read and interpreted as a function prototype!

To fix the problem, there are a few options:
You may add parentheses around argument like this:

vector<string> data((
istream_iterator<string>(cin)),
(istream_iterator<string>()));

You may use the copy initialization:

vector<string> data = vector<string>(
istream_iterator<string>(cin),
istream_iterator<string>());

And if your compiler supports C++0x, you may use initializers list:

vector<string> data{
istream_iterator<string>(cin),
istream_iterator<string>()};

Pattern

One way to avoid this problem happening is to always use copy initialization:

vector<string> data = vector<string>(
istream_iterator<string>(cin),
istream_iterator<string>());

Using this, you are sure it won't look like a function prototype.

Another rule to follow is to write code that uses the vector (like the at() method) after the declaration so you get a compiler error that will help you spot ambiguities.

 

Banner

More Puzzles

Sharpen Your Coding Skills
Vertex Coverings And The Cool Kids Problem

Joe Celko has posed another puzzle that requires you to think like a programmer. This one asks us to find the cool kids in a social network - the ones who taken together know everyone else. This is al [ ... ]


Sharpen Your Coding Skills
Three Warehouses Puzzle

Talking through a problem is often a good way to see what is required for its solution. Reducing it in scale is another good strategy. But as programmers at the end of the day we need to code an algor [ ... ]


C#
Overriding Problems A C# Puzzle

Objects are indispensable but exactly how a language implements them and makes them available to the programmer matters. Sometimes the class-based approach complete with inheritance makes things compl [ ... ]


Other Articles

<ASIN:0132673266>

<ASIN:059600298X>

<ASIN:0321334876>

<ASIN:156592116X>



 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.