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

前端 未结 4 1900
没有蜡笔的小新
没有蜡笔的小新 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:29

    Including the exception specification in the function type is orthogonal to whether a function can call another function with an incompatible exception specification (without handling the exceptions that were not included in its exception specification). The former is about type safety with respect to function pointers (so that you cannot call a throwing function through a function pointer that was known not to throw). Regarding the latter, we can either prohibit it during compilation (as in Java), or treat it as a runtime error (leading to the termination of the program, as is chosen for the C++ current standard).

    One may argue by brining the analogy of calling a non-const (non-static) member function from a const (non-static) member function. However the difference is that indirectly modifying an object inside a non-const member function called through a const member function would go undetected (or else would be too costly to detect) and may lead to nasty bugs, that's why it has to be prevented during compilation. Whereas the act of throwing an exception is (should be) an exceptional event and we can afford inserting run-time checks of whether the exception complies with the exception specification and should be let out, or it violates the program logic and should instead terminate the program.

提交回复
热议问题