What does the C++ compiler do when coming ambiguous default parameters? For example, let's say there was a function such as:
void function(int a = 0, float b = 3.1);
void function(int a, float b =1.1, int c = 0);
Is the above considered ambiguous? If not, what does the compiler do (how is the function matched exactly) when calling something like function1(10)
?
Thanks!
Yes. :-) But not in a way you would expect. Unfortunately, C doesn't allow you to overload methods so you'd end up with two different functions. Still, by calling f2, you'd actually be calling f1 with a default value.
Default Arguments in C++ A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
By default, C uses call by value to pass arguments.
The following is fine
void function(int a = 0, float b = 3.1);
void function(int a, float b =1.1, int c = 0);
And the following is fine too
function(); // calls first function
But the following is ambiguous
function(1); // second and first match equally well
For overload resolution (the process that tells what function to call), parameters that have not passed explicit arguments and that make use of default arguments are ignored. So the compiler really sees two functions both having one int parameter for the above call and can't decide.
The following is ill-formed though
void function(int a = 0, float b = 3.1);
void function(int a, float b =1.1);
While for the code in your question you declare two functions (because both declarations have different number of parameters), in this example you only declare one function. But the second declaration of it repeats a default argument for a parameter (and even with a different value, but that doesn't matter anymore). This is not allowed. Note that the following is fine
void function(int a, float b = 3.1);
void function(int a = 0, float b);
The set of default arguments for declarations that appear in the same scope for the same function are merged, and only for those that appear in the same scope. So the following is valid
void function(int a = 0, float b = 3.1);
void function1() {
void function(int a, float b = 1.1);
function(0);
}
This calls the function with 1.1
passed for b
.
If they have different names (as in your example), there's no ambiguity. If they have the same name (so it's an attempt at an overload), the compiler will complain.
Though it turns out you can redefine the default arguments to a function in a different scope (this is news to me...) - but in the same scope, you can't redefine default arguments even to the same value. from 8.3.6/4 "Default arguments":
For non-template functions, default arguments can be added in later declarations of a function in the same scope. Declarations in different scopes have completely distinct sets of default arguments. That is, declarations in inner scopes do not acquire default arguments from declarations in outer scopes, and vice versa. In a given function declaration, all parameters subsequent to a parameter with a default argument shall have default arguments supplied in this or previous declarations. A default argument shall not be redefined by a later declaration (not even to the same value).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With