简体   繁体   English

operator ++()nothrow无法编译

[英]operator++() nothrow does not compile

Why can't I make operator++() nothrow? 为什么我不能使operator ++()无效?

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. 附带说明一下,postfix运算符也可以设置为线程安全的。

nothrow is a constant used to pass to operator new to indicate that new should not throw an exception on error. nothrow是一个常量,用于传递给new运算符,以指示new不应在出错时引发异常。

I think what you want is noexcept . 我想你想要的就是什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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