I'd like to create a function pointer like this:
void(*function_pointer)()noexcept;
But, this doesn't work. It seems that an exception specifier in a function declaration is invalid. There must be a way to do this though. Right?
This was linked to a question that is not the same as this. Here I'm asking how to create a function pointer with a noexcept specifier. That was not asked or answered in the question "noexcept specifiers in function typedefs".
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.
noexcept (C++) C++11: Specifies whether a function might throw exceptions.
This means that writing a noexcept function is an implicit agreement to the statement : "my program will terminate if any exception is thrown inside this function".
An empty exception specification guarantees that the function does not throw any exception. For example, the function no_problem() guarantees not to throw an exception: extern void no_problem() throw(); If a function declaration does not specify an exception specification, the function can throw exceptions of any type.
[except.spec]/2:
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.
So your declaration is indeed well-formed, and Clang compiles it.
Presumably your compiler is not up to date.
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