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

220 views Asked by At

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!
1

There are 1 answers

2
eq- On BEST ANSWER

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?