在C++ 14及更高版本中,成员函数的constexpr
不再意味着const
。
struct Value
{
int i = 5;
constexpr bool not_five() // requires const to compile
{
return this->i != 5;
}
};
int main()
{
constexpr Value v{6};
static_assert(v.not_five());
}
error: passing ‘const Value’ as ‘this’ argument discards qualifiers [-fpermissive]
static_assert(v.not_five());
^
似乎在编译时调用非const constexpr成员函数意味着常量的突变,因为对其调用的对象在编译时存在并且正在被突变。在什么情况下,非const constexpr成员函数的概念有用吗?
最佳答案
看来我无意中复制了this question和this other one。根据他们的回答,在(至少)两种具体情况下,非const constexpr成员函数很有用。
struct Value
{
int i{};
constexpr Value add(int n)
{
this->i += n;
return *this;
}
constexpr Value add(Value other) const
{
Value ret{this->i};
ret.add(other.i); // call to non-const constexpr member
return ret;
}
};
int main()
{
constexpr Value v = Value{}.add(1);
// v.add(1); // illegal
constexpr Value u = v.add(v);
}
关于c++ - 非const constexpr成员函数的用例?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59186141/