简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM