在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 questionthis 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);
    }
    

    关于c++ - 非const constexpr成员函数的用例?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59186141/

    10-11 22:23
    查看更多