How to call a function with default parameter through a pointer to function that is the return of another function?Howto: c++ Function Pointer with default valuesHow do function pointers in C work?pointers to functionsWhere to put default parameter value in C++?Function pointer with undetermined parameterWhat does it mean to have a function pointer type?Passing a pointer to a function while some (not all) arguments are specifiedtemplate function pointer parameter in a class methodMeaning of int (*) (int *) = 5 (or any integer value)Passing parameters to function pointerPrint Object that is pointed to

How do I know how many sub-shells deep I am?

Why do many websites hide input when entering a OTP

What is /dev/null and why can't I use hx on it?

How fast are we moving relative to the CMB?

Is right click on tables bad UX

What are the most important factors in determining how fast technology progresses?

The answer is the same (tricky puzzle!)

Redirect output on-the-fly - looks not possible in Linux, why?

Does the DOJ's declining to investigate the Trump-Zelensky call ruin the basis for impeachment?

Would Great Old Ones care about the Blood War?

How to tension rope between two trees?

What is this huge bat-winged creature?

Should I be able to see patterns in a HS256 encoded JWT?

Coffee Grounds and Gritty Butter Cream Icing

Was Smaug sealed inside the Lonely Mountain?

Choice of solvent during thin layer chromatography

Is insurance company’s preferred auto shop biased?

As an interviewer, how to conduct interviews with candidates you already know will be rejected?

SHA3-255, one bit less

Found a minor bug, affecting 1% of users. What should QA do?

Using the Grappler feat, can you grapple and pin (restrain) in the same action?

How are characteristic classes morphisms of infinite loop spaces? (if they are)

When can this condition on linear codes be satisfied?

Conveying the idea of "down the road" (i.e. in the future)



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


Howto: c++ Function Pointer with default valuesHow do function pointers in C work?pointers to functionsWhere to put default parameter value in C++?Function pointer with undetermined parameterWhat does it mean to have a function pointer type?Passing a pointer to a function while some (not all) arguments are specifiedtemplate function pointer parameter in a class methodMeaning of int (*) (int *) = 5 (or any integer value)Passing parameters to function pointerPrint Object that is pointed to






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









21















I have functions Mult, Add, Div, Sub, Mod those takes two integers and returns the result of its parameters. And a function Calc that takes a character as an Operator and returns a pointer to function that returns an integer and takes two integer parameters like Mult.



  • Functions like Mult's second parameter is default So when I call Calc, Calc returns the address of Mult or Add... depending on the value of parameter of Calc thus I can pass only one argument.

But It doesn't work with pointer to function:



int Add(int x, int y = 2) // y is default
return x + y;


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


int Div(int x, int y = 2) // y is default
return y ? x / y : -1;


int Sub(int x, int y = 2) // y is default
return x - y;


int Mod(int x, int y = 2) // y is default
return y ? x % y : -1;


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


pFn Calc(char c)
switch (c)
case '+':
return Add;
case '*':
return Mult;
case '/':
return Div;
case '-':
return Sub;
case '%':
return Mod;

return Mult;


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

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

func = Calc('/'); // ok
cout << func(75, 12) << endl; // ok

std::cout << std::endl;



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





















  • 3





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

    – scohe001
    Apr 16 at 20:23






  • 1





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

    – TrebledJ
    Apr 16 at 20:23












  • @scohe001: In a real example, for example depending on the value of the parameter of Double the function returns a pointer to a function from multiple choices: e.g: switch(x) case 1: return Mult; break; case 2: return Add;.

    – Syfu_H
    Apr 16 at 23:08






  • 1





    @Syfu_H But not the type. I don't know C++ very well, but using pFn = int(*)(int, int = 2); or something like that might work.

    – glglgl
    Apr 18 at 19:44






  • 1





    @Syfu_H Ah, ok. Didn't know that, thank you.

    – glglgl
    Apr 19 at 6:57

















21















I have functions Mult, Add, Div, Sub, Mod those takes two integers and returns the result of its parameters. And a function Calc that takes a character as an Operator and returns a pointer to function that returns an integer and takes two integer parameters like Mult.



  • Functions like Mult's second parameter is default So when I call Calc, Calc returns the address of Mult or Add... depending on the value of parameter of Calc thus I can pass only one argument.

But It doesn't work with pointer to function:



int Add(int x, int y = 2) // y is default
return x + y;


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


int Div(int x, int y = 2) // y is default
return y ? x / y : -1;


int Sub(int x, int y = 2) // y is default
return x - y;


int Mod(int x, int y = 2) // y is default
return y ? x % y : -1;


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


pFn Calc(char c)
switch (c)
case '+':
return Add;
case '*':
return Mult;
case '/':
return Div;
case '-':
return Sub;
case '%':
return Mod;

return Mult;


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

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

func = Calc('/'); // ok
cout << func(75, 12) << endl; // ok

std::cout << std::endl;



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





















  • 3





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

    – scohe001
    Apr 16 at 20:23






  • 1





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

    – TrebledJ
    Apr 16 at 20:23












  • @scohe001: In a real example, for example depending on the value of the parameter of Double the function returns a pointer to a function from multiple choices: e.g: switch(x) case 1: return Mult; break; case 2: return Add;.

    – Syfu_H
    Apr 16 at 23:08






  • 1





    @Syfu_H But not the type. I don't know C++ very well, but using pFn = int(*)(int, int = 2); or something like that might work.

    – glglgl
    Apr 18 at 19:44






  • 1





    @Syfu_H Ah, ok. Didn't know that, thank you.

    – glglgl
    Apr 19 at 6:57













21












21








21


4






I have functions Mult, Add, Div, Sub, Mod those takes two integers and returns the result of its parameters. And a function Calc that takes a character as an Operator and returns a pointer to function that returns an integer and takes two integer parameters like Mult.



  • Functions like Mult's second parameter is default So when I call Calc, Calc returns the address of Mult or Add... depending on the value of parameter of Calc thus I can pass only one argument.

But It doesn't work with pointer to function:



int Add(int x, int y = 2) // y is default
return x + y;


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


int Div(int x, int y = 2) // y is default
return y ? x / y : -1;


int Sub(int x, int y = 2) // y is default
return x - y;


int Mod(int x, int y = 2) // y is default
return y ? x % y : -1;


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


pFn Calc(char c)
switch (c)
case '+':
return Add;
case '*':
return Mult;
case '/':
return Div;
case '-':
return Sub;
case '%':
return Mod;

return Mult;


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

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

func = Calc('/'); // ok
cout << func(75, 12) << endl; // ok

std::cout << std::endl;



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 functions Mult, Add, Div, Sub, Mod those takes two integers and returns the result of its parameters. And a function Calc that takes a character as an Operator and returns a pointer to function that returns an integer and takes two integer parameters like Mult.



  • Functions like Mult's second parameter is default So when I call Calc, Calc returns the address of Mult or Add... depending on the value of parameter of Calc thus I can pass only one argument.

But It doesn't work with pointer to function:



int Add(int x, int y = 2) // y is default
return x + y;


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


int Div(int x, int y = 2) // y is default
return y ? x / y : -1;


int Sub(int x, int y = 2) // y is default
return x - y;


int Mod(int x, int y = 2) // y is default
return y ? x % y : -1;


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


pFn Calc(char c)
switch (c)
case '+':
return Add;
case '*':
return Mult;
case '/':
return Div;
case '-':
return Sub;
case '%':
return Mod;

return Mult;


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

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

func = Calc('/'); // ok
cout << func(75, 12) << endl; // ok

std::cout << std::endl;



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 Apr 16 at 23:30







Syfu_H

















asked Apr 16 at 20:21









Syfu_HSyfu_H

4441 silver badge15 bronze badges




4441 silver badge15 bronze badges










  • 3





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

    – scohe001
    Apr 16 at 20:23






  • 1





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

    – TrebledJ
    Apr 16 at 20:23












  • @scohe001: In a real example, for example depending on the value of the parameter of Double the function returns a pointer to a function from multiple choices: e.g: switch(x) case 1: return Mult; break; case 2: return Add;.

    – Syfu_H
    Apr 16 at 23:08






  • 1





    @Syfu_H But not the type. I don't know C++ very well, but using pFn = int(*)(int, int = 2); or something like that might work.

    – glglgl
    Apr 18 at 19:44






  • 1





    @Syfu_H Ah, ok. Didn't know that, thank you.

    – glglgl
    Apr 19 at 6:57












  • 3





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

    – scohe001
    Apr 16 at 20:23






  • 1





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

    – TrebledJ
    Apr 16 at 20:23












  • @scohe001: In a real example, for example depending on the value of the parameter of Double the function returns a pointer to a function from multiple choices: e.g: switch(x) case 1: return Mult; break; case 2: return Add;.

    – Syfu_H
    Apr 16 at 23:08






  • 1





    @Syfu_H But not the type. I don't know C++ very well, but using pFn = int(*)(int, int = 2); or something like that might work.

    – glglgl
    Apr 18 at 19:44






  • 1





    @Syfu_H Ah, ok. Didn't know that, thank you.

    – glglgl
    Apr 19 at 6:57







3




3





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

– scohe001
Apr 16 at 20:23





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

– scohe001
Apr 16 at 20:23




1




1





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

– TrebledJ
Apr 16 at 20:23






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

– TrebledJ
Apr 16 at 20:23














@scohe001: In a real example, for example depending on the value of the parameter of Double the function returns a pointer to a function from multiple choices: e.g: switch(x) case 1: return Mult; break; case 2: return Add;.

– Syfu_H
Apr 16 at 23:08





@scohe001: In a real example, for example depending on the value of the parameter of Double the function returns a pointer to a function from multiple choices: e.g: switch(x) case 1: return Mult; break; case 2: return Add;.

– Syfu_H
Apr 16 at 23:08




1




1





@Syfu_H But not the type. I don't know C++ very well, but using pFn = int(*)(int, int = 2); or something like that might work.

– glglgl
Apr 18 at 19:44





@Syfu_H But not the type. I don't know C++ very well, but using pFn = int(*)(int, int = 2); or something like that might work.

– glglgl
Apr 18 at 19:44




1




1





@Syfu_H Ah, ok. Didn't know that, thank you.

– glglgl
Apr 19 at 6:57





@Syfu_H Ah, ok. Didn't know that, thank you.

– glglgl
Apr 19 at 6:57












3 Answers
3






active

oldest

votes


















27
















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

























  • Thank you! I do really appreciate it. Also: I've edited the question. You can edit it. Now I make the function Calc decides which function to return depending on the Character argument passed in to Calc.

    – Syfu_H
    Apr 16 at 23:32






  • 4





    @Syfu_H please dont change the quesiton substantially after you got answers. This was an excellent answer for the original question and actually I think it still is

    – formerlyknownas_463035818
    Apr 17 at 8:22


















9
















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:



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



And by using a variadic lambda (thanks to @Artyer) you do not even have to repeat the default value:



#include <iostream>

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


auto Double()
return [](auto... args) return Mult(args...); ;


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






share|improve this answer



























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

    – ShadowRanger
    Apr 16 at 20:41











  • @ShadowRanger yes, added a note

    – formerlyknownas_463035818
    Apr 16 at 20:45






  • 4





    To not have to repeat the defaults, just forward variadic arguments: return [](auto... args) return Mult(args...); . Or with perfect forwarding (Which is not really necessary here because this just copies ints, but may be for other functions) return [](auto&&... args) noexcept(noexcept(Mult(std::forward<decltype(args)>(args)...))) -> decltype(auto) return Mult(std::forward<decltype(args)>(args)...); ;

    – Artyer
    Apr 16 at 20:49






  • 2





    @Artyer thanks. didnt post the forwarding one, because I would have to understand it first myself and for ints its not really worth the trouble

    – formerlyknownas_463035818
    Apr 16 at 20:57


















2
















If you always have 2 as default argument, you can wrap your function pointer into a simple helper class like this:



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

class pFn

pFn_ ptr;
public:
pFn(pFn_ p) : ptr(p)
int operator()(int x, int y = 2) const
return ptr(x,y);

;


Full working example: https://godbolt.org/z/5r7tZ8






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',
    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/4.0/"u003ecc by-sa 4.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

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    27
















    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

























    • Thank you! I do really appreciate it. Also: I've edited the question. You can edit it. Now I make the function Calc decides which function to return depending on the Character argument passed in to Calc.

      – Syfu_H
      Apr 16 at 23:32






    • 4





      @Syfu_H please dont change the quesiton substantially after you got answers. This was an excellent answer for the original question and actually I think it still is

      – formerlyknownas_463035818
      Apr 17 at 8:22















    27
















    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

























    • Thank you! I do really appreciate it. Also: I've edited the question. You can edit it. Now I make the function Calc decides which function to return depending on the Character argument passed in to Calc.

      – Syfu_H
      Apr 16 at 23:32






    • 4





      @Syfu_H please dont change the quesiton substantially after you got answers. This was an excellent answer for the original question and actually I think it still is

      – formerlyknownas_463035818
      Apr 17 at 8:22













    27














    27










    27









    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 Apr 16 at 20:24









    ShadowRangerShadowRanger

    74.1k6 gold badges77 silver badges121 bronze badges




    74.1k6 gold badges77 silver badges121 bronze badges















    • Thank you! I do really appreciate it. Also: I've edited the question. You can edit it. Now I make the function Calc decides which function to return depending on the Character argument passed in to Calc.

      – Syfu_H
      Apr 16 at 23:32






    • 4





      @Syfu_H please dont change the quesiton substantially after you got answers. This was an excellent answer for the original question and actually I think it still is

      – formerlyknownas_463035818
      Apr 17 at 8:22

















    • Thank you! I do really appreciate it. Also: I've edited the question. You can edit it. Now I make the function Calc decides which function to return depending on the Character argument passed in to Calc.

      – Syfu_H
      Apr 16 at 23:32






    • 4





      @Syfu_H please dont change the quesiton substantially after you got answers. This was an excellent answer for the original question and actually I think it still is

      – formerlyknownas_463035818
      Apr 17 at 8:22
















    Thank you! I do really appreciate it. Also: I've edited the question. You can edit it. Now I make the function Calc decides which function to return depending on the Character argument passed in to Calc.

    – Syfu_H
    Apr 16 at 23:32





    Thank you! I do really appreciate it. Also: I've edited the question. You can edit it. Now I make the function Calc decides which function to return depending on the Character argument passed in to Calc.

    – Syfu_H
    Apr 16 at 23:32




    4




    4





    @Syfu_H please dont change the quesiton substantially after you got answers. This was an excellent answer for the original question and actually I think it still is

    – formerlyknownas_463035818
    Apr 17 at 8:22





    @Syfu_H please dont change the quesiton substantially after you got answers. This was an excellent answer for the original question and actually I think it still is

    – formerlyknownas_463035818
    Apr 17 at 8:22













    9
















    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:



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



    And by using a variadic lambda (thanks to @Artyer) you do not even have to repeat the default value:



    #include <iostream>

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


    auto Double()
    return [](auto... args) return Mult(args...); ;


    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






    share|improve this answer



























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

      – ShadowRanger
      Apr 16 at 20:41











    • @ShadowRanger yes, added a note

      – formerlyknownas_463035818
      Apr 16 at 20:45






    • 4





      To not have to repeat the defaults, just forward variadic arguments: return [](auto... args) return Mult(args...); . Or with perfect forwarding (Which is not really necessary here because this just copies ints, but may be for other functions) return [](auto&&... args) noexcept(noexcept(Mult(std::forward<decltype(args)>(args)...))) -> decltype(auto) return Mult(std::forward<decltype(args)>(args)...); ;

      – Artyer
      Apr 16 at 20:49






    • 2





      @Artyer thanks. didnt post the forwarding one, because I would have to understand it first myself and for ints its not really worth the trouble

      – formerlyknownas_463035818
      Apr 16 at 20:57















    9
















    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:



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



    And by using a variadic lambda (thanks to @Artyer) you do not even have to repeat the default value:



    #include <iostream>

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


    auto Double()
    return [](auto... args) return Mult(args...); ;


    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






    share|improve this answer



























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

      – ShadowRanger
      Apr 16 at 20:41











    • @ShadowRanger yes, added a note

      – formerlyknownas_463035818
      Apr 16 at 20:45






    • 4





      To not have to repeat the defaults, just forward variadic arguments: return [](auto... args) return Mult(args...); . Or with perfect forwarding (Which is not really necessary here because this just copies ints, but may be for other functions) return [](auto&&... args) noexcept(noexcept(Mult(std::forward<decltype(args)>(args)...))) -> decltype(auto) return Mult(std::forward<decltype(args)>(args)...); ;

      – Artyer
      Apr 16 at 20:49






    • 2





      @Artyer thanks. didnt post the forwarding one, because I would have to understand it first myself and for ints its not really worth the trouble

      – formerlyknownas_463035818
      Apr 16 at 20:57













    9














    9










    9









    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:



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



    And by using a variadic lambda (thanks to @Artyer) you do not even have to repeat the default value:



    #include <iostream>

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


    auto Double()
    return [](auto... args) return Mult(args...); ;


    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






    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:



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



    And by using a variadic lambda (thanks to @Artyer) you do not even have to repeat the default value:



    #include <iostream>

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


    auto Double()
    return [](auto... args) return Mult(args...); ;


    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 16 at 20:51

























    answered Apr 16 at 20:37









    formerlyknownas_463035818formerlyknownas_463035818

    25k4 gold badges33 silver badges82 bronze badges




    25k4 gold badges33 silver badges82 bronze badges















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

      – ShadowRanger
      Apr 16 at 20:41











    • @ShadowRanger yes, added a note

      – formerlyknownas_463035818
      Apr 16 at 20:45






    • 4





      To not have to repeat the defaults, just forward variadic arguments: return [](auto... args) return Mult(args...); . Or with perfect forwarding (Which is not really necessary here because this just copies ints, but may be for other functions) return [](auto&&... args) noexcept(noexcept(Mult(std::forward<decltype(args)>(args)...))) -> decltype(auto) return Mult(std::forward<decltype(args)>(args)...); ;

      – Artyer
      Apr 16 at 20:49






    • 2





      @Artyer thanks. didnt post the forwarding one, because I would have to understand it first myself and for ints its not really worth the trouble

      – formerlyknownas_463035818
      Apr 16 at 20:57

















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

      – ShadowRanger
      Apr 16 at 20:41











    • @ShadowRanger yes, added a note

      – formerlyknownas_463035818
      Apr 16 at 20:45






    • 4





      To not have to repeat the defaults, just forward variadic arguments: return [](auto... args) return Mult(args...); . Or with perfect forwarding (Which is not really necessary here because this just copies ints, but may be for other functions) return [](auto&&... args) noexcept(noexcept(Mult(std::forward<decltype(args)>(args)...))) -> decltype(auto) return Mult(std::forward<decltype(args)>(args)...); ;

      – Artyer
      Apr 16 at 20:49






    • 2





      @Artyer thanks. didnt post the forwarding one, because I would have to understand it first myself and for ints its not really worth the trouble

      – formerlyknownas_463035818
      Apr 16 at 20:57
















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

    – ShadowRanger
    Apr 16 at 20:41





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

    – ShadowRanger
    Apr 16 at 20:41













    @ShadowRanger yes, added a note

    – formerlyknownas_463035818
    Apr 16 at 20:45





    @ShadowRanger yes, added a note

    – formerlyknownas_463035818
    Apr 16 at 20:45




    4




    4





    To not have to repeat the defaults, just forward variadic arguments: return [](auto... args) return Mult(args...); . Or with perfect forwarding (Which is not really necessary here because this just copies ints, but may be for other functions) return [](auto&&... args) noexcept(noexcept(Mult(std::forward<decltype(args)>(args)...))) -> decltype(auto) return Mult(std::forward<decltype(args)>(args)...); ;

    – Artyer
    Apr 16 at 20:49





    To not have to repeat the defaults, just forward variadic arguments: return [](auto... args) return Mult(args...); . Or with perfect forwarding (Which is not really necessary here because this just copies ints, but may be for other functions) return [](auto&&... args) noexcept(noexcept(Mult(std::forward<decltype(args)>(args)...))) -> decltype(auto) return Mult(std::forward<decltype(args)>(args)...); ;

    – Artyer
    Apr 16 at 20:49




    2




    2





    @Artyer thanks. didnt post the forwarding one, because I would have to understand it first myself and for ints its not really worth the trouble

    – formerlyknownas_463035818
    Apr 16 at 20:57





    @Artyer thanks. didnt post the forwarding one, because I would have to understand it first myself and for ints its not really worth the trouble

    – formerlyknownas_463035818
    Apr 16 at 20:57











    2
















    If you always have 2 as default argument, you can wrap your function pointer into a simple helper class like this:



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

    class pFn

    pFn_ ptr;
    public:
    pFn(pFn_ p) : ptr(p)
    int operator()(int x, int y = 2) const
    return ptr(x,y);

    ;


    Full working example: https://godbolt.org/z/5r7tZ8






    share|improve this answer





























      2
















      If you always have 2 as default argument, you can wrap your function pointer into a simple helper class like this:



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

      class pFn

      pFn_ ptr;
      public:
      pFn(pFn_ p) : ptr(p)
      int operator()(int x, int y = 2) const
      return ptr(x,y);

      ;


      Full working example: https://godbolt.org/z/5r7tZ8






      share|improve this answer



























        2














        2










        2









        If you always have 2 as default argument, you can wrap your function pointer into a simple helper class like this:



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

        class pFn

        pFn_ ptr;
        public:
        pFn(pFn_ p) : ptr(p)
        int operator()(int x, int y = 2) const
        return ptr(x,y);

        ;


        Full working example: https://godbolt.org/z/5r7tZ8






        share|improve this answer













        If you always have 2 as default argument, you can wrap your function pointer into a simple helper class like this:



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

        class pFn

        pFn_ ptr;
        public:
        pFn(pFn_ p) : ptr(p)
        int operator()(int x, int y = 2) const
        return ptr(x,y);

        ;


        Full working example: https://godbolt.org/z/5r7tZ8







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 17 at 11:46









        chtzchtz

        9,6442 gold badges14 silver badges38 bronze badges




        9,6442 gold badges14 silver badges38 bronze badges































            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

            Tamil (spriik) Luke uk diar | Nawigatjuun

            Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

            Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?