Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function vs variable declaration in C++

This code works:

std::ifstream f(mapFilename.c_str());
std::string s = std::string(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
ParseGameState(s);

Whereby mapFilename is an std::string and void ParseGameState(const std::string&);.

And this does not:

std::ifstream f(mapFilename.c_str());
std::string s(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
ParseGameState(s);

This is the error:

game.cpp: In member function ‘int Game::LoadMapFromFile(const std::string&)’:
game.cpp:423: error: no matching function for call to ‘ParseGameState(std::string (&)(std::istreambuf_iterator<char, std::char_traits<char> >, std::istreambuf_iterator<char, std::char_traits<char> > (*)()))’
game.cpp:363: note: candidates are: ParseGameState(const std::string&)

So it seems that it recognizes s as a function declaration and not a variable declaration in this case.

Why is that? Is this a bug in GCC 4.2.1 (Apple build)? Or does GCC handles this correctly? Is this undefined in the C++ standard?

like image 906
Albert Avatar asked Sep 12 '10 15:09

Albert


People also ask

What is the difference between variable and function in C?

A variable stores a value, and a function is a program (can't think of another word for it). So you can have a variable of n which stores the value 1, and you can have a function called print(n) that will prints whatever is inside the parenthesis, (n in this example) so the value stored of n is printed.

What is the difference between definition and declaration of a variable and a function?

Declaration means that variable is only declared and memory is allocated, but no value is set. However, definition means the variables has been initialized. The same works for variables, arrays, collections, etc.

How declaration of a function is different from its definition?

Difference Between Definition and Declaration It aims at specifying the name of any given class, function, variable, etc. Definition allocates memory to an entity. A declaration does not allocate memory to the entities.

What is variable declaration in C?

Variables are containers for storing data values. In C, there are different types of variables (defined with different keywords), for example: int - stores integers (whole numbers), without decimals, such as 123 or -123. float - stores floating point numbers, with decimals, such as 19.99 or -19.99.


1 Answers

This is C++'s "most vexing parse." A quick Google for that should turn up lots of hits with lots of details. The basic answer is that yes, the compiler is treating it as a function declaration -- and C++ requires that it do so. There's nothing wrong with your compiler (at least in this respect).

If it's any comfort, you have lots of good company in having run into this. In fact, it's sufficiently common that C++0x is added a new brace-initializer syntax, in large part because it avoids this ambiguity. Using it, you could write something like:

std::string s{std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>()};

That will make it clear that the contents of the braces are intended to be values for initializing s, not types of parameters to a function named s. I don't know if Apple has a port of it yet, but gcc accepts the new syntax as of version 4.5 (or so).

Edit: Rereading N3092, Johannes is (as usual) quite correct. The applicable language is (§8.5.4/3/5): "If T has an initializer-list constructor, the argument list consists of the initializer list as a single argument; otherwise, the argument list consists of the elements of the initializer list."

So, since std::string has an initializer-list constructor, this would attempt to "stuff" the two istreambuf_iterators into an initializer list, and pass that to the std::string ctor that takes an initializer list -- but that would be a type mismatch, so the code can't compile. For some other type type that (unlike std::string did not have an initializer-list ctor) the transformation above would work (thanks to the "otherwise..." in the quote above). In the case of std::string, you'd have to use one of the current alternatives such as std::string s = std:string(...).

I apologize for the incorrect suggested fix -- in this case, one that's all the worse because it confuses an issue that will probably be excessively confusing on its own, and if anything will need careful clarification, especially over the next few years.

like image 127
Jerry Coffin Avatar answered Oct 21 '22 10:10

Jerry Coffin