Kal*_*ish 6 c++ gcc constructor constexpr c++14
如本页所述,constexpr
构造函数体的复合语句,如果没有删除也没有默认,必须满足constexpr
函数体的约束,也就是说,它可以包含除以下内容之外的任何语句:
asm
声明goto
声明try
block似乎标准不限制return
可能出现的语句数量,而在C++ 11中,只允许一个.
现在,请考虑以下代码:
class Thing
{
public:
// Shouldn't this constructor be fine under both C++11 and C++14?
constexpr Thing ( )
{
return;
}
};
int main ( )
{
Thing a_nice_thing;
}
Run Code Online (Sandbox Code Playgroud)
Clang(3.5和-std = c ++ 14)编译得很好,但是GCC(4.9.1和-std = c ++ 14)没有抱怨:
constexpr构造函数没有空体
但是,如果它改变了:
class Thing
{
public:
// This constructor is fine under both C++11 and C++14
constexpr Thing ( )
{
static_assert( __cplusplus > 1 , "static_assert isn't the right mechanism to test this, since it wasn't available at earlier versions of the language" );
}
};
int main ( )
{
Thing a_nice_thing;
}
Run Code Online (Sandbox Code Playgroud)
然后它在两个编译器下编译得很好.
由于海湾合作委员会抱怨建设者的身体不是空的,在后一种情况下难道不应该抱怨吗?这种行为是GCC中的一个错误吗?constexpr构造函数是否允许返回语句?
注意:单个return
陈述是否真的值得这不是这个问题的范围,虽然有趣并且可能值得另一个.return
出于样式原因,我将单个语句放在主体为空的构造函数上.
归档时间: |
|
查看次数: |
2102 次 |
最近记录: |