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.
c++
add a comment |
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.
c++
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 callb.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
add a comment |
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.
c++
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++
c++
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 callb.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
add a comment |
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 callb.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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 19 at 14:34
felix
1,428314
1,428314
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 19 at 14:28
Lightness Races in Orbit
280k51453768
280k51453768
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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