怎样避免编译器把对象声明当作函数声明

解决办法么。。括号去掉。Test test;即可。如果是模板怕indeterminate value就T t = T();----------------------实际上关于歧义的描述在8.2。。ISO C++11 8.21 The ambiguity arising from the similarity between a function-style cast and a declaration mentioned in 6.8 can also occur in the context of a declaration. In that context, the choice is between a function declaration with a redundant set of parentheses around a parameter name and an object declaration with a function-style cast as the initializer. Just as for the ambiguities mentioned in 6.8, the resolution is to consider any construct that could possibly be a declaration a declaration. “the resolution is to consider any construct that could possibly be a declaration a declaration.”
■网友
给 @Sunchy321补充一下,C++11标准中6.8节有此表述。 【怎样避免编译器把对象声明当作函数声明】 怎样避免编译器把对象声明当作函数声明

在C++语法中表达式语句和声明存在二义性。如果一个表达式语句的最左子表达式是函数式显示转换(就是本题中构造函数的写法),则无法将其和以左括号开始的声明(即参数表)区分。在这种情况下认为这是声明。为了消除二义性,必须检查整个语句来决定是表达式语句还是声明。下面的例子中前三个是表达式,后四个为声明。


    推荐阅读