How to call a function with default parameter through a pointer to function that is the return of another function? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live! Should we burninate the [wrap] tag?Howto: c++ Function Pointer with default valuesHow do function pointers in C work?How come pointer to a function be called without dereferencing?Pointer to a C++ class member function as a global function's parameter?What's an effective way to parse command line parameters in C++?What does it mean to have a function pointer type?Passing a pointer to a function while some (not all) arguments are specifiedtype of function pointerHow to include arguments in a function pointer?C Pass arguments as void-pointer-list to imported function from LoadLibrary()Passing parameters to function pointer

Extract all GPU name, model and GPU ram

Identify plant with long narrow paired leaves and reddish stems

2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?

illegal generic type for instanceof when using local classes

Why do people hide their license plates in the EU?

Why aren't air breathing engines used as small first stages

What does this icon in iOS Stardew Valley mean?

How to tell that you are a giant?

When is phishing education going too far?

Using audio cues to encourage good posture

3 doors, three guards, one stone

What is Arya's weapon design?

List *all* the tuples!

Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?

Simplicity of the roots of a minimal polynomial

Should I discuss the type of campaign with my players?

What is the role of the transistor and diode in a soft start circuit?

What LEGO pieces have "real-world" functionality?

String `!23` is replaced with `docker` in command line

What does the "x" in "x86" represent?

Why did the Falcon Heavy center core fall off the ASDS OCISLY barge?

What's the meaning of 間時肆拾貳 at a car parking sign

How does the particle を relate to the verb 行く in the structure「A を + B に行く」?

Book where humans were engineered with genes from animal species to survive hostile planets



How to call a function with default parameter through a pointer to function that is the return of another function?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!
Should we burninate the [wrap] tag?Howto: c++ Function Pointer with default valuesHow do function pointers in C work?How come pointer to a function be called without dereferencing?Pointer to a C++ class member function as a global function's parameter?What's an effective way to parse command line parameters in C++?What does it mean to have a function pointer type?Passing a pointer to a function while some (not all) arguments are specifiedtype of function pointerHow to include arguments in a function pointer?C Pass arguments as void-pointer-list to imported function from LoadLibrary()Passing parameters to function pointer



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








6















I have a function Mult that takes two integers and returns the product of its parameters. And a function Double that takes an integer and returns a pointer to function that returns an integer and takes two integer parameters like Mult.




  • Mult's second parameter is default So when I call Double, Double returns the address of Mult thus I can pass only one argument.

But It doesn't work with pointer to function:



int Mult(int x, int y = 2) // y is default
return x * y;


using pFn = int(*)(int, int);


pFn Double(int x)
return Mult;


int main(int argc, char* argv[])

pFn func = Double(0);
cout << func(7, 4) << endl; // ok
//cout << func(7) << endl; // error: Too few arguments
cout << Mult(4) << endl; // ok. the second argument is default




Above if I call Mult with a single argument it works fine because the second argument is default but calling it through the pointer func it fails. func is pointer to function that takes two integers and returns an int.










share|improve this question



















  • 1





    What is the point of Double taking an integer parameter that it doesn't use?

    – scohe001
    24 mins ago






  • 1





    Similar: Howto: c++ Function Pointer with default values

    – TrebledJ
    24 mins ago


















6















I have a function Mult that takes two integers and returns the product of its parameters. And a function Double that takes an integer and returns a pointer to function that returns an integer and takes two integer parameters like Mult.




  • Mult's second parameter is default So when I call Double, Double returns the address of Mult thus I can pass only one argument.

But It doesn't work with pointer to function:



int Mult(int x, int y = 2) // y is default
return x * y;


using pFn = int(*)(int, int);


pFn Double(int x)
return Mult;


int main(int argc, char* argv[])

pFn func = Double(0);
cout << func(7, 4) << endl; // ok
//cout << func(7) << endl; // error: Too few arguments
cout << Mult(4) << endl; // ok. the second argument is default




Above if I call Mult with a single argument it works fine because the second argument is default but calling it through the pointer func it fails. func is pointer to function that takes two integers and returns an int.










share|improve this question



















  • 1





    What is the point of Double taking an integer parameter that it doesn't use?

    – scohe001
    24 mins ago






  • 1





    Similar: Howto: c++ Function Pointer with default values

    – TrebledJ
    24 mins ago














6












6








6


3






I have a function Mult that takes two integers and returns the product of its parameters. And a function Double that takes an integer and returns a pointer to function that returns an integer and takes two integer parameters like Mult.




  • Mult's second parameter is default So when I call Double, Double returns the address of Mult thus I can pass only one argument.

But It doesn't work with pointer to function:



int Mult(int x, int y = 2) // y is default
return x * y;


using pFn = int(*)(int, int);


pFn Double(int x)
return Mult;


int main(int argc, char* argv[])

pFn func = Double(0);
cout << func(7, 4) << endl; // ok
//cout << func(7) << endl; // error: Too few arguments
cout << Mult(4) << endl; // ok. the second argument is default




Above if I call Mult with a single argument it works fine because the second argument is default but calling it through the pointer func it fails. func is pointer to function that takes two integers and returns an int.










share|improve this question
















I have a function Mult that takes two integers and returns the product of its parameters. And a function Double that takes an integer and returns a pointer to function that returns an integer and takes two integer parameters like Mult.




  • Mult's second parameter is default So when I call Double, Double returns the address of Mult thus I can pass only one argument.

But It doesn't work with pointer to function:



int Mult(int x, int y = 2) // y is default
return x * y;


using pFn = int(*)(int, int);


pFn Double(int x)
return Mult;


int main(int argc, char* argv[])

pFn func = Double(0);
cout << func(7, 4) << endl; // ok
//cout << func(7) << endl; // error: Too few arguments
cout << Mult(4) << endl; // ok. the second argument is default




Above if I call Mult with a single argument it works fine because the second argument is default but calling it through the pointer func it fails. func is pointer to function that takes two integers and returns an int.







c++ function-pointers default-arguments






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 20 mins ago









ShadowRanger

64.1k661100




64.1k661100










asked 27 mins ago









Syfu_HSyfu_H

1506




1506







  • 1





    What is the point of Double taking an integer parameter that it doesn't use?

    – scohe001
    24 mins ago






  • 1





    Similar: Howto: c++ Function Pointer with default values

    – TrebledJ
    24 mins ago













  • 1





    What is the point of Double taking an integer parameter that it doesn't use?

    – scohe001
    24 mins ago






  • 1





    Similar: Howto: c++ Function Pointer with default values

    – TrebledJ
    24 mins ago








1




1





What is the point of Double taking an integer parameter that it doesn't use?

– scohe001
24 mins ago





What is the point of Double taking an integer parameter that it doesn't use?

– scohe001
24 mins ago




1




1





Similar: Howto: c++ Function Pointer with default values

– TrebledJ
24 mins ago






Similar: Howto: c++ Function Pointer with default values

– TrebledJ
24 mins ago













2 Answers
2






active

oldest

votes


















9














Defaulted arguments are a bit of C++ syntactic sugar; when calling the function directly with insufficient arguments, the compiler inserts the default as if the caller had passed it explicitly, so the function is still called with the full complement of arguments (Mult(4) is compiled into the same code as Mult(4, 2) in this case).



The default isn't actually part of the function type though, so you can't use the default for an indirect call; the syntactic sugar breaks down there, since as soon as you are calling through a pointer, the information about the defaults is lost.






share|improve this answer






























    1














    For the "why not" I refer you to this answer. If you want to somehow keep the ability to use a default, you need to provide something more than a function pointer, eg a lamdba will do:



    #include <iostream>

    int Mult(int x, int y = 2) // y is default
    return x * y;


    auto Double()
    return [](int x,int y=2) return Mult(x,y); ;


    int main(int argc, char* argv[])
    auto func = Double();
    std::cout << func(7, 4) << 'n'; // ok
    std::cout << func(7) << 'n'; // ok
    std::cout << Mult(4) << 'n'; // ok



    Live demo



    Take the example with some grain of salt. It is merely meant to demonstrate that it is possible to do something like this. The usefulness of this approach is rather limited. You have to repeat the default parameters in Double to make it work. This is however a general problem with default parameters. They dont really like to be forwarded or be nested. Consider for example



    int Double(int x,int y = ?!? ) // How to make y default here ?!?
    return Mult(x,y); // just call Mult and return result






    share|improve this answer

























    • Note that this involves repeating the default explicitly inside Double when defining the lambda, which limits the utility significantly.

      – ShadowRanger
      6 mins ago











    • @ShadowRanger yes, added a note

      – user463035818
      2 mins ago











    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    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%2f55716044%2fhow-to-call-a-function-with-default-parameter-through-a-pointer-to-function-that%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    9














    Defaulted arguments are a bit of C++ syntactic sugar; when calling the function directly with insufficient arguments, the compiler inserts the default as if the caller had passed it explicitly, so the function is still called with the full complement of arguments (Mult(4) is compiled into the same code as Mult(4, 2) in this case).



    The default isn't actually part of the function type though, so you can't use the default for an indirect call; the syntactic sugar breaks down there, since as soon as you are calling through a pointer, the information about the defaults is lost.






    share|improve this answer



























      9














      Defaulted arguments are a bit of C++ syntactic sugar; when calling the function directly with insufficient arguments, the compiler inserts the default as if the caller had passed it explicitly, so the function is still called with the full complement of arguments (Mult(4) is compiled into the same code as Mult(4, 2) in this case).



      The default isn't actually part of the function type though, so you can't use the default for an indirect call; the syntactic sugar breaks down there, since as soon as you are calling through a pointer, the information about the defaults is lost.






      share|improve this answer

























        9












        9








        9







        Defaulted arguments are a bit of C++ syntactic sugar; when calling the function directly with insufficient arguments, the compiler inserts the default as if the caller had passed it explicitly, so the function is still called with the full complement of arguments (Mult(4) is compiled into the same code as Mult(4, 2) in this case).



        The default isn't actually part of the function type though, so you can't use the default for an indirect call; the syntactic sugar breaks down there, since as soon as you are calling through a pointer, the information about the defaults is lost.






        share|improve this answer













        Defaulted arguments are a bit of C++ syntactic sugar; when calling the function directly with insufficient arguments, the compiler inserts the default as if the caller had passed it explicitly, so the function is still called with the full complement of arguments (Mult(4) is compiled into the same code as Mult(4, 2) in this case).



        The default isn't actually part of the function type though, so you can't use the default for an indirect call; the syntactic sugar breaks down there, since as soon as you are calling through a pointer, the information about the defaults is lost.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 23 mins ago









        ShadowRangerShadowRanger

        64.1k661100




        64.1k661100























            1














            For the "why not" I refer you to this answer. If you want to somehow keep the ability to use a default, you need to provide something more than a function pointer, eg a lamdba will do:



            #include <iostream>

            int Mult(int x, int y = 2) // y is default
            return x * y;


            auto Double()
            return [](int x,int y=2) return Mult(x,y); ;


            int main(int argc, char* argv[])
            auto func = Double();
            std::cout << func(7, 4) << 'n'; // ok
            std::cout << func(7) << 'n'; // ok
            std::cout << Mult(4) << 'n'; // ok



            Live demo



            Take the example with some grain of salt. It is merely meant to demonstrate that it is possible to do something like this. The usefulness of this approach is rather limited. You have to repeat the default parameters in Double to make it work. This is however a general problem with default parameters. They dont really like to be forwarded or be nested. Consider for example



            int Double(int x,int y = ?!? ) // How to make y default here ?!?
            return Mult(x,y); // just call Mult and return result






            share|improve this answer

























            • Note that this involves repeating the default explicitly inside Double when defining the lambda, which limits the utility significantly.

              – ShadowRanger
              6 mins ago











            • @ShadowRanger yes, added a note

              – user463035818
              2 mins ago















            1














            For the "why not" I refer you to this answer. If you want to somehow keep the ability to use a default, you need to provide something more than a function pointer, eg a lamdba will do:



            #include <iostream>

            int Mult(int x, int y = 2) // y is default
            return x * y;


            auto Double()
            return [](int x,int y=2) return Mult(x,y); ;


            int main(int argc, char* argv[])
            auto func = Double();
            std::cout << func(7, 4) << 'n'; // ok
            std::cout << func(7) << 'n'; // ok
            std::cout << Mult(4) << 'n'; // ok



            Live demo



            Take the example with some grain of salt. It is merely meant to demonstrate that it is possible to do something like this. The usefulness of this approach is rather limited. You have to repeat the default parameters in Double to make it work. This is however a general problem with default parameters. They dont really like to be forwarded or be nested. Consider for example



            int Double(int x,int y = ?!? ) // How to make y default here ?!?
            return Mult(x,y); // just call Mult and return result






            share|improve this answer

























            • Note that this involves repeating the default explicitly inside Double when defining the lambda, which limits the utility significantly.

              – ShadowRanger
              6 mins ago











            • @ShadowRanger yes, added a note

              – user463035818
              2 mins ago













            1












            1








            1







            For the "why not" I refer you to this answer. If you want to somehow keep the ability to use a default, you need to provide something more than a function pointer, eg a lamdba will do:



            #include <iostream>

            int Mult(int x, int y = 2) // y is default
            return x * y;


            auto Double()
            return [](int x,int y=2) return Mult(x,y); ;


            int main(int argc, char* argv[])
            auto func = Double();
            std::cout << func(7, 4) << 'n'; // ok
            std::cout << func(7) << 'n'; // ok
            std::cout << Mult(4) << 'n'; // ok



            Live demo



            Take the example with some grain of salt. It is merely meant to demonstrate that it is possible to do something like this. The usefulness of this approach is rather limited. You have to repeat the default parameters in Double to make it work. This is however a general problem with default parameters. They dont really like to be forwarded or be nested. Consider for example



            int Double(int x,int y = ?!? ) // How to make y default here ?!?
            return Mult(x,y); // just call Mult and return result






            share|improve this answer















            For the "why not" I refer you to this answer. If you want to somehow keep the ability to use a default, you need to provide something more than a function pointer, eg a lamdba will do:



            #include <iostream>

            int Mult(int x, int y = 2) // y is default
            return x * y;


            auto Double()
            return [](int x,int y=2) return Mult(x,y); ;


            int main(int argc, char* argv[])
            auto func = Double();
            std::cout << func(7, 4) << 'n'; // ok
            std::cout << func(7) << 'n'; // ok
            std::cout << Mult(4) << 'n'; // ok



            Live demo



            Take the example with some grain of salt. It is merely meant to demonstrate that it is possible to do something like this. The usefulness of this approach is rather limited. You have to repeat the default parameters in Double to make it work. This is however a general problem with default parameters. They dont really like to be forwarded or be nested. Consider for example



            int Double(int x,int y = ?!? ) // How to make y default here ?!?
            return Mult(x,y); // just call Mult and return result







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 mins ago

























            answered 10 mins ago









            user463035818user463035818

            19.3k42971




            19.3k42971












            • Note that this involves repeating the default explicitly inside Double when defining the lambda, which limits the utility significantly.

              – ShadowRanger
              6 mins ago











            • @ShadowRanger yes, added a note

              – user463035818
              2 mins ago

















            • Note that this involves repeating the default explicitly inside Double when defining the lambda, which limits the utility significantly.

              – ShadowRanger
              6 mins ago











            • @ShadowRanger yes, added a note

              – user463035818
              2 mins ago
















            Note that this involves repeating the default explicitly inside Double when defining the lambda, which limits the utility significantly.

            – ShadowRanger
            6 mins ago





            Note that this involves repeating the default explicitly inside Double when defining the lambda, which limits the utility significantly.

            – ShadowRanger
            6 mins ago













            @ShadowRanger yes, added a note

            – user463035818
            2 mins ago





            @ShadowRanger yes, added a note

            – user463035818
            2 mins ago

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55716044%2fhow-to-call-a-function-with-default-parameter-through-a-pointer-to-function-that%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Isabella Eugénie Boyer Biographie | Références | Menu de navigationmodifiermodifier le codeComparator to Compute the Relative Value of a U.S. Dollar Amount – 1774 to Present.

            Join wedge with single bond in chemfigHow to make only one part of double bond bold with chemfig?Crossing bonds in chemfigjoining atoms in chemfig. Two adjacent molculesHow do I selectively change bond length in chemfig?Ugly bond joints in chemfigchemfig: reaction above arrowUsing the mhchem and chemfig packages in conjunctionBonding to specific element letter using chemfigResonance hybrids in chemfigScale chemfig molecule in beamer with tikzWhy does this chemfig bond with a hook start in the middle of the atom?

            Are small insurances worth itIs insurance worth it if you can afford to replace the item? If not, when is it?Is accident insurance worth it for my kids who play sportsIs insuring property for more than it is worth allowed?At what point does it become worth it to file an insurance claim?Are wage loss insurance programs worth the cost compared to having an emergency fund?When is an event worth insuring against?Is insurance worth it if you can afford to replace the item? If not, when is it?FHA loan just commenced : Any way to get any of the up-front mortgage insurance back?Which types of insurances do I need to buy?Should I carry less renter's insurance if I can self-insure?Mortgage Adviser Signed Me Up For Multiple Home and Life Insurances (UK)Why many travel insurances don't cover country of nationality?