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

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question

















  • 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






  • 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














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?










share|improve this question

















  • 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






  • 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












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?










share|improve this question













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++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 30 at 20:36









Krystian S

1269




1269







  • 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






  • 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




    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




    @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












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.






share|improve this answer




















  • 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

















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”. ...







share|improve this answer





























    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"



    ...







    share|improve this answer




















      Your Answer





      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      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






























      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.






      share|improve this answer




















      • 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














      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.






      share|improve this answer




















      • 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












      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.






      share|improve this answer












      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.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      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
















      • 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












      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”. ...







      share|improve this answer


























        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”. ...







        share|improve this answer
























          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”. ...







          share|improve this answer














          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”. ...








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 1 at 18:37

























          answered Sep 30 at 20:48









          user2079303

          70.2k550109




          70.2k550109




















              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"



              ...







              share|improve this answer
























                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"



                ...







                share|improve this answer






















                  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"



                  ...







                  share|improve this answer













                  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"



                  ...








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 30 at 20:48









                  Edgar Rokjān

                  14.8k42751




                  14.8k42751



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      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













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)