How does this example work in the standard section 6.8?

In the standard section ยง6.8 of the standard (draft N3690) I see this strange piece of code:

struct T2 { T2(int){ } };
int a, (*(*b)(T2))(int), c, d;

      

What is int(*(*b)(T2))(int)

?!

Is a b

pointer to a constructor T2

?! or maybe a pointer to a function pointer?

The strange thing is that the following code compiles fine too:

struct T2 { T2(int){ } };
int((*b)(T2));

      

+3


source to share


3 answers


int (*(*b)(T2))(int) 

      

It declares b

as a pointer to a function that:

  • takes T2

    as argument
  • and returns a pointer to a function that
    • takes int

      as argument
    • and returns int

      .

You should simplify this expression by using typedefs like:

typedef int (*return_type) (int);
typedef return_type(*function_type) (T2);

      



Or is it better to use C ++ 11 type aliases:

using return_type   = int(*)(int);
using function_type = return_type(*)(T2);

      

Then the declaration will become:

function_type b; //so simple!

      

Hope it helps.

+9


source


This is a variable declaration b

. b

is a pointer to a function with one parameter of type T2. The return type of this function is a function pointer with one parameter, int, returning an int.



+1


source


This will help you understand what's going on here:

struct T2 { T2(int){ } };

typedef int (*func)(int);

int
(*foo(T2))(int) {
}

func
bar(T2) {
}

int main() {
    int (*(*b)(T2))(int);

    b = &foo;
    b = &bar;

    return 0;
}

      

So this is a function that takes T2 and returns a pointer to a function that returns an int and takes an int as a parameter.

I'll add this to my example of why c (++) is a terrible language.

By the way, you cannot take the address of the constructor (C ++ 98 Standard 12.1 / 12 Constructors - "12.1-12 Constructors -" The address of the constructor should not be accepted.)

0


source







All Articles