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;
};
余談ですが、後置演算子もスレッドセーフにすることができます。