Search code examples
c++exceptionfunction-pointersc++17noexcept

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


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 can't tell whether this will prevent noexcept(true) functions from still being able to call noexcept(false) functions.

Will the following code still be valid for C++17?

void will_throw() noexcept(false){
  throw 0;
}

void will_not_throw() noexcept(true){
  will_throw();
}

Solution

  • According to cppreference:

    Note that a noexcept specification on a function is not a compile-time check; it is merely a method for a programmer to inform the compiler whether or not a function should throw exceptions.

    So the syntax of your code is valid, but std::terminate will be called when executed.