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










share|improve this question

























    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?










    share|improve this question























      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?










      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 1 hour ago









      Krystian S

      867




      867






















          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.






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

















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







          share|improve this answer





























            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"



            ...







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






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














              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.






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












              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.






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
















              • 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












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







              share|improve this answer


























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







                share|improve this answer
























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







                  share|improve this answer














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








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 46 mins ago

























                  answered 57 mins ago









                  user2079303

                  69.7k550109




                  69.7k550109




















                      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"



                      ...







                      share|improve this answer
























                        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"



                        ...







                        share|improve this answer






















                          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"



                          ...







                          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 57 mins ago









                          Edgar Rokjān

                          14.4k42650




                          14.4k42650



























                               

                              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













































































                              Comments

                              Popular posts from this blog

                              Long meetings (6-7 hours a day): Being “babysat” by supervisor

                              Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                              Confectionery