Are noexcept specifiers accepted in function typedefs?
as in:
typedef void (*fptr)() noexcept;
Intuitively, noexcept specifiers seem to make sense since they would allow some optimisations at the caller's side.
I got a mixed answer from gcc 4.6.1.
typedef void (*fptr)() noexcept;
results in: error: ‘fptr’ declared with an exception specification
but:
template<void (*FPtr)() noexcept>
struct A{};
compiles without warning.
The noexcept operator performs a compile-time check that returns true if an expression is declared to not throw any exceptions. It can be used within a function template's noexcept specifier to declare that the function will throw exceptions for some types but not others.
In contrast, noexcept(false) means that the function may throw an exception. The noexcept specification is part of the function type but can not be used for function overloading. There are two good reasons for the use of noexcept: First, an exception specifier documents the behaviour of the function.
Note that noexcept doesn't actually prevent the function from throwing exceptions or calling other functions that are potentially throwing. Rather, when an exception is thrown, if an exception exits a noexcept function, std::terminate will be called.
Explicit instantiations may use the noexcept specifier, but it is not required. If used, the exception specification must be the same as for all other declarations.
clang gives:
test.cpp:1:25: error: exception specifications are not allowed in typedefs
typedef void (*fptr)() noexcept;
^
1 error generated.
This is backed up in the C++11 standard in 15.4 [except.spec]/p2:
... An exception-specification shall not appear in a typedef declaration or alias-declaration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With