operator ++()nothrow不编译

Dam*_*ian 1 c++ nothrow

为什么我不能使operator ++()nothrow?

这可能是使用postfix ++运算符(通过前缀++运算符)的少数几个优点之一.

例如,此代码无法编译

class Number
{
public:
    Number& operator++ ()     // ++ prefix
    {
        ++m_c;
        return *this;
    }

    Number operator++ (int) nothrow  // postfix ++
    {
        Number result(*this);   // make a copy for result
        ++(*this);              // Now use the prefix version to do the work
        return result;          // return the copy (the old) value.
    }

    int m_c;
};
Run Code Online (Sandbox Code Playgroud)

另外注意,后缀运算符也可以是线程安全的.

zda*_*dan 6

nothrow 是一个常量,用于传递给operator new,表示new不应该在出错时抛出异常.

我想你想要的是noexcept.