-4

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.

shaiwali
  • 53
  • 1
  • 5
  • 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 '18 at 14:24
  • What's the error message? Probably it cannot decide between the two valid overloads? – Matthieu Brucher Nov 19 '18 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. https://stackoverflow.com/review/suggested-edits/21455932 – StoryTeller - Unslander Monica Nov 19 '18 at 14:28
  • @SHR Please be more careful when submitting edits. Heed the warnings you're given. – Lightness Races in Orbit Nov 19 '18 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 '18 at 14:31

2 Answers2

9

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.

Community
  • 1
  • 1
felix
  • 2,213
  • 7
  • 16
5

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055