Visual Studio C ++编译器怪异的行为 [英] Visual Studio C++ compiler weird behaviour
问题描述
我只是想知道为什么这小段代码在 Visual Studio 中正确编译(并且没有警告)。也许结果与 GCC 和 Clang ,但很遗憾,我现在无法测试。
struct T {
int t;
T():t(0){}
};
int main(){
T(i_do_not_exist);
return 0;
}
T(i_do_not_exist);
是与 T i_do_not_exist;
相同含义的对象声明。
N4567§6.8 [stmt.ambig] p1
语法中含有 expression-statement 和声明 s:具有函数式显式类型转换(5.2.3)的表达式语句作为其最左子表达式可以与声明,其中第一个声明符以
$(
)开头,在这种情况下, > 。
在声明中
TD
其中D
的格式为
(D1)
所包含的声明器类型-id 与声明中所包含的 declarator-id 相同
T D1
括号不会改变嵌入的 declarator-id 类型,
I'm just curious to know why this small piece of code compiles correctly (and without warnings) in Visual Studio. Maybe the result is the same with GCC and Clang, but unfortunately I can't test them now.
struct T { int t; T() : t(0) {} }; int main() { T(i_do_not_exist); return 0; }
解决方案
T(i_do_not_exist);
is an object declaration with the same meaning asT i_do_not_exist;
.N4567 § 6.8[stmt.ambig]p1
There is an ambiguity in the grammar involving expression-statements and declarations: An expression-statement with a function-style explicit type conversion (5.2.3) 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.§ 8.3[dcl.meaning]p6
In a declaration
T D
whereD
has the form
( D1 )
the type of the contained declarator-id is the same as that of the contained declarator-id in the declaration
T D1
Parentheses do not alter the type of the embedded declarator-id, but they can alter the binding of complex declarators.
这篇关于Visual Studio C ++编译器怪异的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!