In C++14 Sec 15.4;2 it is stated, that ... An exception-specification shall not appear in a typedef declaration or alias-declaration.
That means the following is forbidden:
typedef void (*fn)(int) noexcept;
But does the the wording shall not appear mean the token noexcept
cannot appear anywhere in a typedef declaration?
For instance are these both are forbidden as well?
typedef void (*fn1)(void (*)(int) noexcept);
typedef decltype(std::declval<void (*)(int)noexcept>()) fn2;
These both try to define a type fn1
and fn2
being able to point to a function that takes a pointer to a function taking an int
and returning nothing while promising to never throw an exception.
So in my examples the exception-specification is not applied to the toplevel type fn1
resp. fn2
that are introduced by typedef
but to the parameters these function may receive.
So should I take 15.4;2 verbatim and therefore my both examples are invalid? Or is only application to a type-definition forbidden and my examples are correct?
Exception specifications are a C++ language feature that indicate the programmer's intent about the exception types that can be propagated by a function. You can specify that a function may or may not exit by an exception by using an exception specification.
Explanation: C++ provides a mechanism to ensure that a given function is limited to throwing only a specified list of exceptions. It is called an exception specification.
A dynamic exception specification whose set of adjusted types is empty (after any packs are expanded) (since C++11) is non-throwing. A function with a non-throwing dynamic exception specification does not allow any exceptions. A dynamic exception specification is not considered part of a function's type.
Answer. exception specification is a way to restrict a function to throw only certain specified exceptions. The general form of using an exception specification is : type function (argument list) throw (type-list)
It's the whole thing.
Quoting the full wording of [except.spec]/p2
from C++11 and C++14:
An exception-specification shall appear only on a function declarator for a function type, pointer to function type, reference to function type, or pointer to member function type that is the top-level type of a declaration or definition, or on such a type appearing as a parameter or return type in a function declarator. An exception-specification shall not appear in a typedef declaration or alias-declaration.
Perhaps it's not completely clear, but I think it's sufficiently so: you can use an exception specification on a function declaration, including in a parameter or return type on a function declaration … but not in an alias declaration.
This wording has been completely removed in C++17; noexcept
is part of the type system now.
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