Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a noexcept function pointer?

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".

like image 683
Michael Gazonda Avatar asked Dec 15 '14 16:12

Michael Gazonda


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 is Noexcept keyword?

noexcept (C++) C++11: Specifies whether a function might throw exceptions.

What happens when Noexcept function throws?

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".

Which of the function declarations ensure that it will not throw an exception?

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.


1 Answers

[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.

like image 178
Columbo Avatar answered Sep 20 '22 01:09

Columbo