在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成员函数很有用。
临时变量。允许在编译时对临时值进行突变;它仅在分配时变为常数。
[在函数执行过程中对象的更改。在执行过程中,constexpr函数可能会创建一个对象实例,然后通过成员函数对其进行突变。除非它们是constexpr,否则不能在编译时调用这些成员函数,但是如果它们是const,则它们无法更改要针对其调用的实例。
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);
}