As an interesting follow-up (not of big practical importance though) to my previous question: Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
I found out that combining the declaration in parentheses with injected class name feature may lead to surprising results regarding compiler behavior.
Take a look at the following program:
#include <iostream>
struct B
{
};
struct C
{
C (){ std::cout << "C" << '\n'; }
C (B *) { std::cout << "C (B *)" << '\n';}
};
B *y = nullptr;
int main()
{
C::C (y);
}
Compiling with g++ 4.9.2 gives me the following compilation error:
main.cpp:16:10: error: cannot call constructor 'C::C' directly [-fpermissive]
It compiles successfully with MSVC2013/2015 and prints
C (B *)
It compiles successfully with clang 3.5 and prints
C
So obligatory question is which one is right? :)
(I strongly swayed towards clang version though and msvc way to stop declaring variable after just changing type with technically its typedef seems kind of weird)
C::C y;
doesn't make sense, right? Neither doesC::C (y);
At first I thought this was an instance of Most-Vexing-Parse stackoverflow.com/questions/tagged/most-vexing-parse, but now I think it's just undefined behavior meaning all three compilers are "right." – Dale Wilson Apr 16 '15 at 17:09C::C
does not name a type it names a function, so GCC is right imo. – Galik Apr 16 '15 at 17:15