Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ , function pointer exception error

I get "error: exception specifications are not allowed beyond a single level of indirection" with the following code. Please point me to the part of standard that says that it is not allowed. I want to be sure that it is really required by the language or just compiler specific error. If it is from language specification, what motivates this rule? I am using clang 3.8.0.

  int main()
    {
        void (**fp)() throw() ;
    }
like image 368
qqqqq Avatar asked Jun 05 '17 20:06

qqqqq


People also ask

Can we have a pointer to a function?

A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions. You cannot perform pointer arithmetic on pointers to functions.

What is function pointer in C with example?

1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code. 2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers. 3) A function's name can also be used to get functions' address.

Are function pointers safe?

They are potentially dangerous if misused since program execution can go to unwanted location. In fact, coding standards like MISRA C (e.g. rule 104) forbid the use of variable function pointers. If you are using function pointers, it can be tricky to figure out where they are pointing to.

Why function pointer is used in C?

Function pointers in C can be used to create function calls to which they point. This allows programmers to pass them to functions as arguments. Such functions passed as an argument to other functions are also called callback functions.


1 Answers

You said:

Please point me to a reference book /spec that says that it is not allowed. I want to be sure that it is really required by the language or just compiler specific error.

With

void (**fp)() throw() ;

you are trying to specify an exception-specification in the declaration of a pointer to a function pointer. That is not allowed by the standard. Exception-specification is allowed only for a limited set of declarations.

From https://timsong-cpp.github.io/cppwp/n3337/except.spec#2 (emphasis mine):

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. [ 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 example ] A type denoted in an exception-specification shall not denote an incomplete type. A type denoted in an exception-specification shall not denote a pointer or reference to an incomplete type, other than void*, const void*, volatile void*, or const volatile void*. A type cv T, “array of T”, or “function returning T” denoted in an exception-specification is adjusted to type T, “pointer to T”, or “pointer to function returning T”, respectively.


You asked:

If it is from language specification, what motivates this rule?

I don't have an answer to that.

like image 137
R Sahu Avatar answered Sep 18 '22 12:09

R Sahu