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));
source to share
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
.
- takes
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.
source to share
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.)
source to share