direct initialization of a POD variable does not work but copy initialization does when pushing the variable onto a vector

StackOverflow https://stackoverflow.com/questions/11994243

Question

Why does the following code fail to compile, while the two examples after compile successfully? I'm using VS 2008 on Windows 7.


Direct initialization of POD (fails):

int pod();
std::vector<int> pods;
//pods.push_back(pod); // This will generate a compiler error
// Compile error: 1>c:\test.hpp(43) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'int (__cdecl *)(void)' to 'const int &'

Copy initialization of POD (succeeds:

int pod = int();
std::vector<int> pods;
pods.push_back(pod); // No error!
Was it helpful?

Solution

Look up "most vexing parse" (it has been discussed over and over again here, too).

int pod(); // this does not declare (nor define) an integer

By the way, why did you put that 1 in the MyClass example?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top