【问题标题】:C++ throw() (_NOEXCEPT) after function declaration函数声明后的 C++ throw() (_NOEXCEPT)
【发布时间】:2014-01-22 02:02:48
【问题描述】:

我曾在 Visual C++ 中看到包含文件 <vector> 在函数后使用 throw()

size_type capacity() const _NOEXCEPT
    {   // return current length of allocated storage
    return (this->_Myend - this->_Myfirst);
    }

_NOEXCEPTthrow() 的宏,所以上面看起来像:

size_type capacity() const throw()
    {   // return current length of allocated storage
    return (this->_Myend - this->_Myfirst);
    }

但是投掷有什么作用呢?我在this 中看到过问题,为什么这是一种不好的做法,但为什么在没有抛出或捕获的情况下将它放在那里?

【问题讨论】:

标签: c++ function throw noexcept


【解决方案1】:

抛出异常规范在 C++11 中已弃用并由 noexcept 取代。

来自http://en.cppreference.com/w/cpp/language/noexcept_spec

noexcept 是 throw() 的改进版本,在 C++11。与 throw() 不同,noexcept 不会调用 std::unexpected 并且可能 或者可能不会展开堆栈,这可能允许编译器 在没有 throw() 运行时开销的情况下实现 noexcept。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 2019-10-20
    相关资源
    最近更新 更多