Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

noexcept specifiers in function typedefs

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.

like image 578
mirk Avatar asked Oct 26 '11 14:10

mirk


People also ask

What is Noexcept specifier in C++?

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.

What does it mean to specify a function as Noexcept false?

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.

What happens when Noexcept function throws?

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.

Is Noexcept required?

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.


1 Answers

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.

like image 134
Howard Hinnant Avatar answered Oct 12 '22 16:10

Howard Hinnant