> Why does C++ allow us to surround the variable name in parentheses when declaring a variable?2个
看完路易斯·布兰迪在CppCon 2017上的演讲后,我惊讶地发现这段代码实际编译:
#include <string> int main() { std::string(foo); return 0; }
由于某种原因,std :: string(foo)它与std :: string foo相同,即声明一个变量.我发现它绝对违反直觉,并且看不出C以这种方式工作的任何理由.我希望这会给出关于未定义标识符foo的错误.
它实际上使像token1(token2)这样的表达式具有比我之前想象的更多可能的解释.
所以我的问题是:这种恐怖的原因是什么?这个规则什么时候真的有必要?
附:抱歉这个措辞不好的标题,请随时改变它!
解决方法
There is an ambiguity in the grammar involving expression-statements and declarations: An expression-statement with a function-style explicit type conversion as its leftmost subexpression can be indistinguishable from a declaration where the first declarator starts with a
(
. In those cases the statement is a declaration.
并且,类似地,对于功能,在[dcl.ambig.res]:
The ambiguity arising from the similarity between a function-style cast and a declaration mentioned in [stmt.ambig] 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 [stmt.ambig],the resolution is to consider any construct that could possibly be a declaration a declaration.
因此:
Why oh why is
std::string("foo")
so different fromstd::string(foo)
前者不能是宣言.后者可以是一个声明,带有一组冗余的括号.因此,前者不是宣言而后者是宣言.
根本的问题是,语法,声明者可以从a开始(这可能使它与函数式显式类型转换无法区分.而不是提出任意复杂的规则来试图确定用户的意思,语言只选择一个,并且用户可以轻松修复代码以实际执行他的意思.