Function overloading with default parameter in c++











up vote
-4
down vote

favorite












My program is:



class base {
int a, b;
public:
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c=0 ){
return a + b + c;
}
};

int main() {

base b;
b.add(10, 20);//compilation error
return 0;
}


I know there is ambiguity arise. But I want to know how a compiler works for a default argument with function overloading. Because if I'm not calling this function or call add(10,20,0) it works fine.



or



for function overloading it is treated as add(int,int),add(int ,int, int)because it has 2 different function signature but what logic behind that please explain in detail.










share|improve this question




















  • 2




    "But I want to know how a compiler works for a default argument with function overloading" Well, it's clearly not working, as you're getting a compilation error. If you call b.add(10, 20);, how would it know which of the two functions you wanted?
    – Blaze
    Nov 19 at 14:24












  • What's the error message? Probably it cannot decide between the two valid overloads?
    – Matthieu Brucher
    Nov 19 at 14:24






  • 2




    @SHR - There was a substantial edit proposed, fixing all the grammar and spelling, that you just trampled over to make the first line bold. stackoverflow.com/review/suggested-edits/21455932
    – StoryTeller
    Nov 19 at 14:28












  • @SHR Please be more careful when submitting edits. Heed the warnings you're given.
    – Lightness Races in Orbit
    Nov 19 at 14:29










  • Problem is if I am not using that default argument it is working fine because of diff signature I know how the compiler treats that but my question is in which phase compiler understand it is ambiguity arise because for overloading the definition is correct only at a time of calling it shows error.
    – shaiwali
    Nov 19 at 14:31















up vote
-4
down vote

favorite












My program is:



class base {
int a, b;
public:
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c=0 ){
return a + b + c;
}
};

int main() {

base b;
b.add(10, 20);//compilation error
return 0;
}


I know there is ambiguity arise. But I want to know how a compiler works for a default argument with function overloading. Because if I'm not calling this function or call add(10,20,0) it works fine.



or



for function overloading it is treated as add(int,int),add(int ,int, int)because it has 2 different function signature but what logic behind that please explain in detail.










share|improve this question




















  • 2




    "But I want to know how a compiler works for a default argument with function overloading" Well, it's clearly not working, as you're getting a compilation error. If you call b.add(10, 20);, how would it know which of the two functions you wanted?
    – Blaze
    Nov 19 at 14:24












  • What's the error message? Probably it cannot decide between the two valid overloads?
    – Matthieu Brucher
    Nov 19 at 14:24






  • 2




    @SHR - There was a substantial edit proposed, fixing all the grammar and spelling, that you just trampled over to make the first line bold. stackoverflow.com/review/suggested-edits/21455932
    – StoryTeller
    Nov 19 at 14:28












  • @SHR Please be more careful when submitting edits. Heed the warnings you're given.
    – Lightness Races in Orbit
    Nov 19 at 14:29










  • Problem is if I am not using that default argument it is working fine because of diff signature I know how the compiler treats that but my question is in which phase compiler understand it is ambiguity arise because for overloading the definition is correct only at a time of calling it shows error.
    – shaiwali
    Nov 19 at 14:31













up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











My program is:



class base {
int a, b;
public:
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c=0 ){
return a + b + c;
}
};

int main() {

base b;
b.add(10, 20);//compilation error
return 0;
}


I know there is ambiguity arise. But I want to know how a compiler works for a default argument with function overloading. Because if I'm not calling this function or call add(10,20,0) it works fine.



or



for function overloading it is treated as add(int,int),add(int ,int, int)because it has 2 different function signature but what logic behind that please explain in detail.










share|improve this question















My program is:



class base {
int a, b;
public:
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c=0 ){
return a + b + c;
}
};

int main() {

base b;
b.add(10, 20);//compilation error
return 0;
}


I know there is ambiguity arise. But I want to know how a compiler works for a default argument with function overloading. Because if I'm not calling this function or call add(10,20,0) it works fine.



or



for function overloading it is treated as add(int,int),add(int ,int, int)because it has 2 different function signature but what logic behind that please explain in detail.







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 14:33

























asked Nov 19 at 14:21









shaiwali

84




84








  • 2




    "But I want to know how a compiler works for a default argument with function overloading" Well, it's clearly not working, as you're getting a compilation error. If you call b.add(10, 20);, how would it know which of the two functions you wanted?
    – Blaze
    Nov 19 at 14:24












  • What's the error message? Probably it cannot decide between the two valid overloads?
    – Matthieu Brucher
    Nov 19 at 14:24






  • 2




    @SHR - There was a substantial edit proposed, fixing all the grammar and spelling, that you just trampled over to make the first line bold. stackoverflow.com/review/suggested-edits/21455932
    – StoryTeller
    Nov 19 at 14:28












  • @SHR Please be more careful when submitting edits. Heed the warnings you're given.
    – Lightness Races in Orbit
    Nov 19 at 14:29










  • Problem is if I am not using that default argument it is working fine because of diff signature I know how the compiler treats that but my question is in which phase compiler understand it is ambiguity arise because for overloading the definition is correct only at a time of calling it shows error.
    – shaiwali
    Nov 19 at 14:31














  • 2




    "But I want to know how a compiler works for a default argument with function overloading" Well, it's clearly not working, as you're getting a compilation error. If you call b.add(10, 20);, how would it know which of the two functions you wanted?
    – Blaze
    Nov 19 at 14:24












  • What's the error message? Probably it cannot decide between the two valid overloads?
    – Matthieu Brucher
    Nov 19 at 14:24






  • 2




    @SHR - There was a substantial edit proposed, fixing all the grammar and spelling, that you just trampled over to make the first line bold. stackoverflow.com/review/suggested-edits/21455932
    – StoryTeller
    Nov 19 at 14:28












  • @SHR Please be more careful when submitting edits. Heed the warnings you're given.
    – Lightness Races in Orbit
    Nov 19 at 14:29










  • Problem is if I am not using that default argument it is working fine because of diff signature I know how the compiler treats that but my question is in which phase compiler understand it is ambiguity arise because for overloading the definition is correct only at a time of calling it shows error.
    – shaiwali
    Nov 19 at 14:31








2




2




"But I want to know how a compiler works for a default argument with function overloading" Well, it's clearly not working, as you're getting a compilation error. If you call b.add(10, 20);, how would it know which of the two functions you wanted?
– Blaze
Nov 19 at 14:24






"But I want to know how a compiler works for a default argument with function overloading" Well, it's clearly not working, as you're getting a compilation error. If you call b.add(10, 20);, how would it know which of the two functions you wanted?
– Blaze
Nov 19 at 14:24














What's the error message? Probably it cannot decide between the two valid overloads?
– Matthieu Brucher
Nov 19 at 14:24




What's the error message? Probably it cannot decide between the two valid overloads?
– Matthieu Brucher
Nov 19 at 14:24




2




2




@SHR - There was a substantial edit proposed, fixing all the grammar and spelling, that you just trampled over to make the first line bold. stackoverflow.com/review/suggested-edits/21455932
– StoryTeller
Nov 19 at 14:28






@SHR - There was a substantial edit proposed, fixing all the grammar and spelling, that you just trampled over to make the first line bold. stackoverflow.com/review/suggested-edits/21455932
– StoryTeller
Nov 19 at 14:28














@SHR Please be more careful when submitting edits. Heed the warnings you're given.
– Lightness Races in Orbit
Nov 19 at 14:29




@SHR Please be more careful when submitting edits. Heed the warnings you're given.
– Lightness Races in Orbit
Nov 19 at 14:29












Problem is if I am not using that default argument it is working fine because of diff signature I know how the compiler treats that but my question is in which phase compiler understand it is ambiguity arise because for overloading the definition is correct only at a time of calling it shows error.
– shaiwali
Nov 19 at 14:31




Problem is if I am not using that default argument it is working fine because of diff signature I know how the compiler treats that but my question is in which phase compiler understand it is ambiguity arise because for overloading the definition is correct only at a time of calling it shows error.
– shaiwali
Nov 19 at 14:31












2 Answers
2






active

oldest

votes

















up vote
7
down vote













Here is how it(overload resolution) works:




16.3.2.1 From the set of candidate functions constructed for a given context (16.3.1), a set of viable functions is chosen, from which the best function will be selected by comparing argument conversion sequences and associated constraints (17.4.2) for the best fit (16.3.3).



16.3.2.2 First, to be a viable function, a candidate function shall have enough parameters to agree in number with the arguments in the list. [...] A candidate function having more than m parameters is viable only if the (m+1)-st parameter has a default argument (11.3.6). For the purposes of overload resolution, the parameter list is truncated on the right, so that there are exactly m parameters.




So basically, they have identical signature during overload resolution.






share|improve this answer




























    up vote
    5
    down vote













    You have two functions named add. One takes two arguments, and the other takes three arguments. The second function's signature is add(int, int, int), always. It does not have two signatures.



    It's just that if you don't give it that third argument yourself, it's filled in for you.



    Of course, this can only happen when overload resolution has already found add(int, int, int); as you've discovered, in this particular case, it's impossible for the compiler to guess which add you wanted, but if not for the ambiguity then it would be simple. The default argument is basically syntactic sugar, and has no effect on the internals of the function, on its signature, or on its mangled symbol name.






    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: 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%2f53376620%2ffunction-overloading-with-default-parameter-in-c%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








      up vote
      7
      down vote













      Here is how it(overload resolution) works:




      16.3.2.1 From the set of candidate functions constructed for a given context (16.3.1), a set of viable functions is chosen, from which the best function will be selected by comparing argument conversion sequences and associated constraints (17.4.2) for the best fit (16.3.3).



      16.3.2.2 First, to be a viable function, a candidate function shall have enough parameters to agree in number with the arguments in the list. [...] A candidate function having more than m parameters is viable only if the (m+1)-st parameter has a default argument (11.3.6). For the purposes of overload resolution, the parameter list is truncated on the right, so that there are exactly m parameters.




      So basically, they have identical signature during overload resolution.






      share|improve this answer

























        up vote
        7
        down vote













        Here is how it(overload resolution) works:




        16.3.2.1 From the set of candidate functions constructed for a given context (16.3.1), a set of viable functions is chosen, from which the best function will be selected by comparing argument conversion sequences and associated constraints (17.4.2) for the best fit (16.3.3).



        16.3.2.2 First, to be a viable function, a candidate function shall have enough parameters to agree in number with the arguments in the list. [...] A candidate function having more than m parameters is viable only if the (m+1)-st parameter has a default argument (11.3.6). For the purposes of overload resolution, the parameter list is truncated on the right, so that there are exactly m parameters.




        So basically, they have identical signature during overload resolution.






        share|improve this answer























          up vote
          7
          down vote










          up vote
          7
          down vote









          Here is how it(overload resolution) works:




          16.3.2.1 From the set of candidate functions constructed for a given context (16.3.1), a set of viable functions is chosen, from which the best function will be selected by comparing argument conversion sequences and associated constraints (17.4.2) for the best fit (16.3.3).



          16.3.2.2 First, to be a viable function, a candidate function shall have enough parameters to agree in number with the arguments in the list. [...] A candidate function having more than m parameters is viable only if the (m+1)-st parameter has a default argument (11.3.6). For the purposes of overload resolution, the parameter list is truncated on the right, so that there are exactly m parameters.




          So basically, they have identical signature during overload resolution.






          share|improve this answer












          Here is how it(overload resolution) works:




          16.3.2.1 From the set of candidate functions constructed for a given context (16.3.1), a set of viable functions is chosen, from which the best function will be selected by comparing argument conversion sequences and associated constraints (17.4.2) for the best fit (16.3.3).



          16.3.2.2 First, to be a viable function, a candidate function shall have enough parameters to agree in number with the arguments in the list. [...] A candidate function having more than m parameters is viable only if the (m+1)-st parameter has a default argument (11.3.6). For the purposes of overload resolution, the parameter list is truncated on the right, so that there are exactly m parameters.




          So basically, they have identical signature during overload resolution.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 14:34









          felix

          1,428314




          1,428314
























              up vote
              5
              down vote













              You have two functions named add. One takes two arguments, and the other takes three arguments. The second function's signature is add(int, int, int), always. It does not have two signatures.



              It's just that if you don't give it that third argument yourself, it's filled in for you.



              Of course, this can only happen when overload resolution has already found add(int, int, int); as you've discovered, in this particular case, it's impossible for the compiler to guess which add you wanted, but if not for the ambiguity then it would be simple. The default argument is basically syntactic sugar, and has no effect on the internals of the function, on its signature, or on its mangled symbol name.






              share|improve this answer

























                up vote
                5
                down vote













                You have two functions named add. One takes two arguments, and the other takes three arguments. The second function's signature is add(int, int, int), always. It does not have two signatures.



                It's just that if you don't give it that third argument yourself, it's filled in for you.



                Of course, this can only happen when overload resolution has already found add(int, int, int); as you've discovered, in this particular case, it's impossible for the compiler to guess which add you wanted, but if not for the ambiguity then it would be simple. The default argument is basically syntactic sugar, and has no effect on the internals of the function, on its signature, or on its mangled symbol name.






                share|improve this answer























                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  You have two functions named add. One takes two arguments, and the other takes three arguments. The second function's signature is add(int, int, int), always. It does not have two signatures.



                  It's just that if you don't give it that third argument yourself, it's filled in for you.



                  Of course, this can only happen when overload resolution has already found add(int, int, int); as you've discovered, in this particular case, it's impossible for the compiler to guess which add you wanted, but if not for the ambiguity then it would be simple. The default argument is basically syntactic sugar, and has no effect on the internals of the function, on its signature, or on its mangled symbol name.






                  share|improve this answer












                  You have two functions named add. One takes two arguments, and the other takes three arguments. The second function's signature is add(int, int, int), always. It does not have two signatures.



                  It's just that if you don't give it that third argument yourself, it's filled in for you.



                  Of course, this can only happen when overload resolution has already found add(int, int, int); as you've discovered, in this particular case, it's impossible for the compiler to guess which add you wanted, but if not for the ambiguity then it would be simple. The default argument is basically syntactic sugar, and has no effect on the internals of the function, on its signature, or on its mangled symbol name.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 at 14:28









                  Lightness Races in Orbit

                  280k51453768




                  280k51453768






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53376620%2ffunction-overloading-with-default-parameter-in-c%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

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin