1

operator++() を非スローにできないのはなぜですか?

これは、後置 ++ 演算子 (前置 ++ 演算子よりも) を使用する数少ない利点の 1 つです。

たとえば、このコードはコンパイルされません

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

余談ですが、後置演算子もスレッドセーフにすることができます。

4

1 に答える 1

6

nothrownew がエラー時に例外をスローしてはならないことを示すために new 演算子に渡すために使用される定数です。

あなたが望むのはnoexceptだと思います。

于 2016-05-17T00:26:51.737 に答える