Syntax of an un-named function pointer in C++
Clash Royale CLAN TAG#URR8PPP
up vote
6
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
6
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
6
down vote
favorite
up vote
6
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 1 hour ago
Krystian S
867
867
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
5
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
20 mins ago
@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
17 mins ago
add a comment |Â
up vote
2
down vote
There are two sets of parentheses in the declaration. The outer set of parentheses are the argument list of 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
1
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
5
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
20 mins ago
@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
17 mins ago
add a comment |Â
up vote
5
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
20 mins ago
@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
17 mins ago
add a comment |Â
up vote
5
down vote
accepted
up vote
5
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 59 mins ago
melpomene
52.5k53782
52.5k53782
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
20 mins ago
@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
17 mins ago
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
20 mins ago
@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
17 mins ago
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
20 mins ago
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
20 mins ago
@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
17 mins ago
@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
17 mins ago
add a comment |Â
up vote
2
down vote
There are two sets of parentheses in the declaration. The outer set of parentheses are the argument list of 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
There are two sets of parentheses in the declaration. The outer set of parentheses are the argument list of 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
up vote
2
down vote
There are two sets of parentheses in the declaration. The outer set of parentheses are the argument list of 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 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 46 mins ago
answered 57 mins ago
user2079303
69.7k550109
69.7k550109
add a comment |Â
add a comment |Â
up vote
1
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
1
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
1
down vote
up vote
1
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 57 mins ago
Edgar RokjÃÂn
14.4k42650
14.4k42650
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