c++nothrow

operator++() nothrow does not compile


Why can't I make operator++() nothrow?

This could be one of the few advantages of using the postfix ++ operator (over the prefix ++ operator).

For example, this code does not compile

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;
};

On a side note it the postfix operator could also be made thread-safe.


Solution

  • nothrow is a constant used to pass to operator new to indicate that new should not throw an exception on error.

    I think what you want is noexcept.