Can I put a throw declaration in the signature of a typedef function?

Is it possible to declare a pointer to a function, including the throw specification? For example, I have this function:

void without_throw() throw() { } 

And I would like to create a function that takes it as a parameter, complete with the throw() . I tried adding it to typedef , but this does not work:

 typedef void (*without)() throw(); 

GCC gives me the error error: 'without' declared with an exception specification .

+6
source share
2 answers

You cannot dial this. This is clearly not allowed in the standard. (And replacing this with noexcept does not help, the same problem.)

Citation C ++ 11 draft n3290 (Β§15.4 / 2 Exceptions)

An exception specification should only appear in a function declaration for a function type, a pointer to a function type, a reference to a function type, or a pointer to a member function type that is a type of declaration or top-level definition or that type that appears as a parameter or return type in function declaration. An exception specification should not appear in a typedef declaration or alias declaration. [Example:

 void f() throw(int); // OK void (*fp)() throw (int); // OK void g(void pfa() throw(int)); // OK typedef int (*pf)() throw(int); // ill-formed 

- end of example]

The second example allows you to do something like this:

 void foo() throw() {} void bar() {} int main() { void (*fa)() throw() = foo; void (*fb)() throw() = bar; // error, does not compile } 
+7
source

You can also use std :: function if C ++ 0x is acceptable:

 #include <functional> void without_throw() throw() {} typedef std::function<void() throw()> without; int main() { without w = &without_throw; w(); } 
+2
source

Source: https://habr.com/ru/post/977965/


All Articles