C++ ';constexpr';非静态成员函数将不会隐式';常数';在C++;1y;添加';常数';避免行为上的改变
clang++发出以下警告(请参见下面的代码): “constexpr”非静态成员函数将不会隐式为“const” C++1y;添加“const”以避免行为发生变化C++ ';constexpr';非静态成员函数将不会隐式';常数';在C++;1y;添加';常数';避免行为上的改变,c++,constants,clang,constexpr,C++,Constants,Clang,Constexpr,clang++发出以下警告(请参见下面的代码): “constexpr”非静态成员函数将不会隐式为“const” C++1y;添加“const”以避免行为发生变化 const应该添加到哪里const constexpr size\u t getSize(){发出另一个警告: 返回类型上的“const”类型限定符无效 代码: constexpr size_t getSize() { return sizeof(header); } 我相信它告诉您,从C++1y开始,成员函数不能在cons
const
应该添加到哪里const constexpr size\u t getSize(){
发出另一个警告:
返回类型上的“const”类型限定符无效
代码:
constexpr size_t getSize()
{
return sizeof(header);
}
我相信它告诉您,从C++1y开始,成员函数不能在
const
对象上调用
在getSize()
之后添加const
,使其成为const
成员函数:
constexpr size_t getsize() const { ... }
完整的测试用例:
struct S {
constexpr int getSize();
};
诊断:
tmp.cc:2:17: warning: 'constexpr' non-static member function will not be
implicitly 'const' in C++1y; add 'const' to avoid a change in
behavior [-Wconstexpr-not-const]
constexpr int getSize();
^
const
请特别注意输出的底线。这称为“修复提示”,并向您显示需要插入的文本(以及插入位置)以修复问题
(在本例中,文本以前导空格开头,使其不太清楚是否应该插入分号之前,而不是之后。)顺便说一句,下面是一篇解释C++1y更改的文章: