VS2010 has supported the C++11 partially. I compile the code below in VS2010 RTM. I’m confused why the code CLS() is analyzed to different meanings. In the line “decltype(CLS()) obj1;”, the CLS() denotes an class object entity. But in the line “CLS obj2(CLS());”, the CLS() denotes a function pointer, which retuns a CLS object with no parameter. Is the behavior expected? Is it described in the standard?
struct CLS
{
int mi;
};
int _tmain(int argc, _TCHAR* argv[])
{
decltype(CLS()) obj1;
obj1.mi = 10;
CLS obj2(CLS());
obj2.mi = 10; // error C2228: left of '.mi' must have class/struct/union
return 0;
}
UPDATE 12/8/2011
Per C++11 7.1.6.2/1, the expected string in the parenthesis is an expression. The compiler just needs to check if the string can be parsed as a valid expression. If yes, the code is well-formed. So for the code “decltype(CLS()) obj1;”, the “CLS()” is treated as a valid expression which denotes a difinition of object.
decltype-specifier:
decltype ( expression )
UPDATE 1/3/2012
Potatoswatter gives the explanation why “CLS obj2(CLS());” is a declaration other than an object definition.
Anything that may be interpreted as either an expression or a declaration is a declaration, however unusual it may be. CLS obj2( CLS() ); declares a function whose parameter type CLS() is a function with no arguments returning CLS, and whose return type is CLS.
As others have said, this is the Most Vexing Parse. Anything that may be interpreted as either an expression or a declaration is a declaration, however unusual it may be.
CLS obj2( CLS() );declares a function whose parameter typeCLS()is a function with no arguments returning CLS, and whose return type is CLS.For example,
The solution is to use C++11’s uniform initialization:
or simply