Syntax of an un-named function pointer in C++

Clash Royale CLAN TAG#URR8PPP
up vote
12
down vote
favorite
I was looking into most vexing parse, and I stumbled upon something like this:
Foo bar(Baz()); // bar is a function that takes a pointer to a function that returns a Baz and returns a Foo
This is quite different from the typical syntax of return-type(*name)(parameters). Are the parenthesis present the parenthesis for the parameter list, or are they for the name?
c++
add a comment |Â
up vote
12
down vote
favorite
I was looking into most vexing parse, and I stumbled upon something like this:
Foo bar(Baz()); // bar is a function that takes a pointer to a function that returns a Baz and returns a Foo
This is quite different from the typical syntax of return-type(*name)(parameters). Are the parenthesis present the parenthesis for the parameter list, or are they for the name?
c++
1
Oh my C++, syntax can be confusing sometimes. IfBazwere a function of no parameters returning aFoo, then that declaresbaras a variable of typeFooinitialized toBaz(). But ifBazis a type thenbaris declared to be a function?! Crazy....
â Ray Toal
Oct 1 at 1:28
2
@RayToal sorry for the self-promotion, but you might like this parsing abuse I put together last year ;)
â Quentin
Oct 1 at 8:29
Self-promotion appreciated!
â Ray Toal
Oct 1 at 22:44
add a comment |Â
up vote
12
down vote
favorite
up vote
12
down vote
favorite
I was looking into most vexing parse, and I stumbled upon something like this:
Foo bar(Baz()); // bar is a function that takes a pointer to a function that returns a Baz and returns a Foo
This is quite different from the typical syntax of return-type(*name)(parameters). Are the parenthesis present the parenthesis for the parameter list, or are they for the name?
c++
I was looking into most vexing parse, and I stumbled upon something like this:
Foo bar(Baz()); // bar is a function that takes a pointer to a function that returns a Baz and returns a Foo
This is quite different from the typical syntax of return-type(*name)(parameters). Are the parenthesis present the parenthesis for the parameter list, or are they for the name?
c++
c++
asked Sep 30 at 20:36
Krystian S
1269
1269
1
Oh my C++, syntax can be confusing sometimes. IfBazwere a function of no parameters returning aFoo, then that declaresbaras a variable of typeFooinitialized toBaz(). But ifBazis a type thenbaris declared to be a function?! Crazy....
â Ray Toal
Oct 1 at 1:28
2
@RayToal sorry for the self-promotion, but you might like this parsing abuse I put together last year ;)
â Quentin
Oct 1 at 8:29
Self-promotion appreciated!
â Ray Toal
Oct 1 at 22:44
add a comment |Â
1
Oh my C++, syntax can be confusing sometimes. IfBazwere a function of no parameters returning aFoo, then that declaresbaras a variable of typeFooinitialized toBaz(). But ifBazis a type thenbaris declared to be a function?! Crazy....
â Ray Toal
Oct 1 at 1:28
2
@RayToal sorry for the self-promotion, but you might like this parsing abuse I put together last year ;)
â Quentin
Oct 1 at 8:29
Self-promotion appreciated!
â Ray Toal
Oct 1 at 22:44
1
1
Oh my C++, syntax can be confusing sometimes. If
Baz were a function of no parameters returning a Foo, then that declares bar as a variable of type Foo initialized to Baz(). But if Baz is a type then bar is declared to be a function?! Crazy....â Ray Toal
Oct 1 at 1:28
Oh my C++, syntax can be confusing sometimes. If
Baz were a function of no parameters returning a Foo, then that declares bar as a variable of type Foo initialized to Baz(). But if Baz is a type then bar is declared to be a function?! Crazy....â Ray Toal
Oct 1 at 1:28
2
2
@RayToal sorry for the self-promotion, but you might like this parsing abuse I put together last year ;)
â Quentin
Oct 1 at 8:29
@RayToal sorry for the self-promotion, but you might like this parsing abuse I put together last year ;)
â Quentin
Oct 1 at 8:29
Self-promotion appreciated!
â Ray Toal
Oct 1 at 22:44
Self-promotion appreciated!
â Ray Toal
Oct 1 at 22:44
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
16
down vote
accepted
Fully explicit form:
Foo bar(Baz f());
bar is a function that takes a single parameter f, which is a function (taking no arguments) returning Baz.
Without naming the parameter:
Foo bar(Baz ());
The reason bar ends up taking a pointer to a function is that functions cannot be passed by value, so declaring a parameter as a function automatically decays it into a pointer. The above declaration is equivalent to:
Foo bar(Baz (*)());
// or:
Foo bar(Baz (*f)()); // with a named parameter
This is similar to void foo(int [10]) where int [10] also means int * in a parameter list.
Should one syntax be used over another (disregarding the use of std::function)? If i were to have a function pointer as a parameter, should I write a normal function declaration and let it decay to a pointer, or should I just write out a function pointer declaration?
â Krystian S
Sep 30 at 21:25
3
@KrystianS That's a matter of personal taste. I prefer being explicit, so when my parameters are pointers, I declare them as pointers (not as arrays or functions).
â melpomene
Sep 30 at 21:28
add a comment |Â
up vote
5
down vote
There are two sets of parentheses in the declaration. The outer set of parentheses are the argument list of the function bar:
Foo bar(Baz());
^ ^
Baz() in this declaration is a function type. The parentheses in a function type declaration delimit the argument list of that function.
Foo bar(Baz());
^^
To clarify: In the context of a function argument declarator, a function type is adjusted to be a pointer to a function of that type. So the declaration is in fact equivalent to:
Foo bar(Baz(*)());
^ ^
The highlighted parentheses of this alternative pointer argument declarator are not present in the "unadjusted" declaration.
Relevant standard rule:
[dcl.fct]
The type of a function is determined using the following rules.
The type of each parameter (including function parameter packs) is determined from its own decl-specifier-seq and declarator.
After determining the type of each parameter, any parameter of type âÂÂarray of Tâ or of function type T is adjusted to be âÂÂpointer to TâÂÂ. ...
add a comment |Â
up vote
2
down vote
Are the parenthesis present the parenthesis for the parameter list, or are they for the name?
They are for the parameter list.
So:
Foo bar(Baz());
declares a function which accepts a single parameter of a type function which returns Baz and accepts no parameters.
This, in turns, equal to a a function declaration which accepts a single parameter of a type pointer to a function which returns Baz and accepts no parameters. as (from function):
The type of each function parameter in the parameter list is determined according to the following rules:
...
3) If the type is a function type F, it is replaced by the type "pointer to F"
...
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
16
down vote
accepted
Fully explicit form:
Foo bar(Baz f());
bar is a function that takes a single parameter f, which is a function (taking no arguments) returning Baz.
Without naming the parameter:
Foo bar(Baz ());
The reason bar ends up taking a pointer to a function is that functions cannot be passed by value, so declaring a parameter as a function automatically decays it into a pointer. The above declaration is equivalent to:
Foo bar(Baz (*)());
// or:
Foo bar(Baz (*f)()); // with a named parameter
This is similar to void foo(int [10]) where int [10] also means int * in a parameter list.
Should one syntax be used over another (disregarding the use of std::function)? If i were to have a function pointer as a parameter, should I write a normal function declaration and let it decay to a pointer, or should I just write out a function pointer declaration?
â Krystian S
Sep 30 at 21:25
3
@KrystianS That's a matter of personal taste. I prefer being explicit, so when my parameters are pointers, I declare them as pointers (not as arrays or functions).
â melpomene
Sep 30 at 21:28
add a comment |Â
up vote
16
down vote
accepted
Fully explicit form:
Foo bar(Baz f());
bar is a function that takes a single parameter f, which is a function (taking no arguments) returning Baz.
Without naming the parameter:
Foo bar(Baz ());
The reason bar ends up taking a pointer to a function is that functions cannot be passed by value, so declaring a parameter as a function automatically decays it into a pointer. The above declaration is equivalent to:
Foo bar(Baz (*)());
// or:
Foo bar(Baz (*f)()); // with a named parameter
This is similar to void foo(int [10]) where int [10] also means int * in a parameter list.
Should one syntax be used over another (disregarding the use of std::function)? If i were to have a function pointer as a parameter, should I write a normal function declaration and let it decay to a pointer, or should I just write out a function pointer declaration?
â Krystian S
Sep 30 at 21:25
3
@KrystianS That's a matter of personal taste. I prefer being explicit, so when my parameters are pointers, I declare them as pointers (not as arrays or functions).
â melpomene
Sep 30 at 21:28
add a comment |Â
up vote
16
down vote
accepted
up vote
16
down vote
accepted
Fully explicit form:
Foo bar(Baz f());
bar is a function that takes a single parameter f, which is a function (taking no arguments) returning Baz.
Without naming the parameter:
Foo bar(Baz ());
The reason bar ends up taking a pointer to a function is that functions cannot be passed by value, so declaring a parameter as a function automatically decays it into a pointer. The above declaration is equivalent to:
Foo bar(Baz (*)());
// or:
Foo bar(Baz (*f)()); // with a named parameter
This is similar to void foo(int [10]) where int [10] also means int * in a parameter list.
Fully explicit form:
Foo bar(Baz f());
bar is a function that takes a single parameter f, which is a function (taking no arguments) returning Baz.
Without naming the parameter:
Foo bar(Baz ());
The reason bar ends up taking a pointer to a function is that functions cannot be passed by value, so declaring a parameter as a function automatically decays it into a pointer. The above declaration is equivalent to:
Foo bar(Baz (*)());
// or:
Foo bar(Baz (*f)()); // with a named parameter
This is similar to void foo(int [10]) where int [10] also means int * in a parameter list.
answered Sep 30 at 20:46
melpomene
53.2k54084
53.2k54084
Should one syntax be used over another (disregarding the use of std::function)? If i were to have a function pointer as a parameter, should I write a normal function declaration and let it decay to a pointer, or should I just write out a function pointer declaration?
â Krystian S
Sep 30 at 21:25
3
@KrystianS That's a matter of personal taste. I prefer being explicit, so when my parameters are pointers, I declare them as pointers (not as arrays or functions).
â melpomene
Sep 30 at 21:28
add a comment |Â
Should one syntax be used over another (disregarding the use of std::function)? If i were to have a function pointer as a parameter, should I write a normal function declaration and let it decay to a pointer, or should I just write out a function pointer declaration?
â Krystian S
Sep 30 at 21:25
3
@KrystianS That's a matter of personal taste. I prefer being explicit, so when my parameters are pointers, I declare them as pointers (not as arrays or functions).
â melpomene
Sep 30 at 21:28
Should one syntax be used over another (disregarding the use of std::function)? If i were to have a function pointer as a parameter, should I write a normal function declaration and let it decay to a pointer, or should I just write out a function pointer declaration?
â Krystian S
Sep 30 at 21:25
Should one syntax be used over another (disregarding the use of std::function)? If i were to have a function pointer as a parameter, should I write a normal function declaration and let it decay to a pointer, or should I just write out a function pointer declaration?
â Krystian S
Sep 30 at 21:25
3
3
@KrystianS That's a matter of personal taste. I prefer being explicit, so when my parameters are pointers, I declare them as pointers (not as arrays or functions).
â melpomene
Sep 30 at 21:28
@KrystianS That's a matter of personal taste. I prefer being explicit, so when my parameters are pointers, I declare them as pointers (not as arrays or functions).
â melpomene
Sep 30 at 21:28
add a comment |Â
up vote
5
down vote
There are two sets of parentheses in the declaration. The outer set of parentheses are the argument list of the function bar:
Foo bar(Baz());
^ ^
Baz() in this declaration is a function type. The parentheses in a function type declaration delimit the argument list of that function.
Foo bar(Baz());
^^
To clarify: In the context of a function argument declarator, a function type is adjusted to be a pointer to a function of that type. So the declaration is in fact equivalent to:
Foo bar(Baz(*)());
^ ^
The highlighted parentheses of this alternative pointer argument declarator are not present in the "unadjusted" declaration.
Relevant standard rule:
[dcl.fct]
The type of a function is determined using the following rules.
The type of each parameter (including function parameter packs) is determined from its own decl-specifier-seq and declarator.
After determining the type of each parameter, any parameter of type âÂÂarray of Tâ or of function type T is adjusted to be âÂÂpointer to TâÂÂ. ...
add a comment |Â
up vote
5
down vote
There are two sets of parentheses in the declaration. The outer set of parentheses are the argument list of the function bar:
Foo bar(Baz());
^ ^
Baz() in this declaration is a function type. The parentheses in a function type declaration delimit the argument list of that function.
Foo bar(Baz());
^^
To clarify: In the context of a function argument declarator, a function type is adjusted to be a pointer to a function of that type. So the declaration is in fact equivalent to:
Foo bar(Baz(*)());
^ ^
The highlighted parentheses of this alternative pointer argument declarator are not present in the "unadjusted" declaration.
Relevant standard rule:
[dcl.fct]
The type of a function is determined using the following rules.
The type of each parameter (including function parameter packs) is determined from its own decl-specifier-seq and declarator.
After determining the type of each parameter, any parameter of type âÂÂarray of Tâ or of function type T is adjusted to be âÂÂpointer to TâÂÂ. ...
add a comment |Â
up vote
5
down vote
up vote
5
down vote
There are two sets of parentheses in the declaration. The outer set of parentheses are the argument list of the function bar:
Foo bar(Baz());
^ ^
Baz() in this declaration is a function type. The parentheses in a function type declaration delimit the argument list of that function.
Foo bar(Baz());
^^
To clarify: In the context of a function argument declarator, a function type is adjusted to be a pointer to a function of that type. So the declaration is in fact equivalent to:
Foo bar(Baz(*)());
^ ^
The highlighted parentheses of this alternative pointer argument declarator are not present in the "unadjusted" declaration.
Relevant standard rule:
[dcl.fct]
The type of a function is determined using the following rules.
The type of each parameter (including function parameter packs) is determined from its own decl-specifier-seq and declarator.
After determining the type of each parameter, any parameter of type âÂÂarray of Tâ or of function type T is adjusted to be âÂÂpointer to TâÂÂ. ...
There are two sets of parentheses in the declaration. The outer set of parentheses are the argument list of the function bar:
Foo bar(Baz());
^ ^
Baz() in this declaration is a function type. The parentheses in a function type declaration delimit the argument list of that function.
Foo bar(Baz());
^^
To clarify: In the context of a function argument declarator, a function type is adjusted to be a pointer to a function of that type. So the declaration is in fact equivalent to:
Foo bar(Baz(*)());
^ ^
The highlighted parentheses of this alternative pointer argument declarator are not present in the "unadjusted" declaration.
Relevant standard rule:
[dcl.fct]
The type of a function is determined using the following rules.
The type of each parameter (including function parameter packs) is determined from its own decl-specifier-seq and declarator.
After determining the type of each parameter, any parameter of type âÂÂarray of Tâ or of function type T is adjusted to be âÂÂpointer to TâÂÂ. ...
edited Oct 1 at 18:37
answered Sep 30 at 20:48
user2079303
70.2k550109
70.2k550109
add a comment |Â
add a comment |Â
up vote
2
down vote
Are the parenthesis present the parenthesis for the parameter list, or are they for the name?
They are for the parameter list.
So:
Foo bar(Baz());
declares a function which accepts a single parameter of a type function which returns Baz and accepts no parameters.
This, in turns, equal to a a function declaration which accepts a single parameter of a type pointer to a function which returns Baz and accepts no parameters. as (from function):
The type of each function parameter in the parameter list is determined according to the following rules:
...
3) If the type is a function type F, it is replaced by the type "pointer to F"
...
add a comment |Â
up vote
2
down vote
Are the parenthesis present the parenthesis for the parameter list, or are they for the name?
They are for the parameter list.
So:
Foo bar(Baz());
declares a function which accepts a single parameter of a type function which returns Baz and accepts no parameters.
This, in turns, equal to a a function declaration which accepts a single parameter of a type pointer to a function which returns Baz and accepts no parameters. as (from function):
The type of each function parameter in the parameter list is determined according to the following rules:
...
3) If the type is a function type F, it is replaced by the type "pointer to F"
...
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Are the parenthesis present the parenthesis for the parameter list, or are they for the name?
They are for the parameter list.
So:
Foo bar(Baz());
declares a function which accepts a single parameter of a type function which returns Baz and accepts no parameters.
This, in turns, equal to a a function declaration which accepts a single parameter of a type pointer to a function which returns Baz and accepts no parameters. as (from function):
The type of each function parameter in the parameter list is determined according to the following rules:
...
3) If the type is a function type F, it is replaced by the type "pointer to F"
...
Are the parenthesis present the parenthesis for the parameter list, or are they for the name?
They are for the parameter list.
So:
Foo bar(Baz());
declares a function which accepts a single parameter of a type function which returns Baz and accepts no parameters.
This, in turns, equal to a a function declaration which accepts a single parameter of a type pointer to a function which returns Baz and accepts no parameters. as (from function):
The type of each function parameter in the parameter list is determined according to the following rules:
...
3) If the type is a function type F, it is replaced by the type "pointer to F"
...
answered Sep 30 at 20:48
Edgar RokjÃÂn
14.8k42751
14.8k42751
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52581933%2fsyntax-of-an-un-named-function-pointer-in-c%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Oh my C++, syntax can be confusing sometimes. If
Bazwere a function of no parameters returning aFoo, then that declaresbaras a variable of typeFooinitialized toBaz(). But ifBazis a type thenbaris declared to be a function?! Crazy....â Ray Toal
Oct 1 at 1:28
2
@RayToal sorry for the self-promotion, but you might like this parsing abuse I put together last year ;)
â Quentin
Oct 1 at 8:29
Self-promotion appreciated!
â Ray Toal
Oct 1 at 22:44