Can a noexcept function still call a function that throws in C++17?

前端 未结 4 1899
没有蜡笔的小新
没有蜡笔的小新 2021-02-15 13:08

In P0012R1, \"Make exception specifications be part of the type system\",
I see that noexcept is now becoming a part of the function type.

I

4条回答
  •  一向
    一向 (楼主)
    2021-02-15 13:42

    noexcept(true) functions can call noexcept(false) functions. There will be a runtime error if an exception is thrown. The canonical example of why this is allowed is:

    double hypotenuse(double opposite, double adjacent) noexcept(true)
    {
        return std::sqrt(opposite*opposite + adjacent*adjacent);
    }
    

    std::sqrt will throw domain_error if its argument is negative, but clearly that will never happen here.

    (In an ideal world, it would be forbidden by default with an exception_cast to allow it where required. The result could either be UB if an exception is thrown, or std::terminate).

提交回复
热议问题