While trying to answer this question I found without () (which invokes “C++ most vexing parse”) the output of g++ is 1 (Can be seen here: http://ideone.com/GPBHy), where as visual studio gives a linker error. I couldn’t understand how the output can 1, any clues?
While trying to answer this question I found without () (which invokes C++ most
Share
As the answers to the question already explain, due to the “Most Vexing Parse” the statement instead of defining an object named
strwith the twoistream_iteratorsto specify its initializers, is parsed as a declaration of a function namedstrthat returns astring.So a simple version of the program resolves to, this online sample:
Output:
Note that there is no overloaded
operator <<that takes anstd::ostreamand a function pointer as arguments, this is because there can be any number of user defined function types and ofcourse a standard overloaded api cannot account for them all.Given that the compiler tries to find the best match among the existing overloads which happens to be
bool(a function pointer is implicitly convertible tobool[#1]).In particular,
Since the function pointer points to something and not
null, the value is printed as1.[#1]C++03 4.12 Boolean conversions