views:

125

answers:

4

What does the C++ compiler do when coming ambiguous default parameters? For example, let's say there was a function such as:

function1(int a = 0, float b = 3.1);
function2(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!

+1  A: 

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).

Michael Burr
A: 

Ambiguous? You have two completely independent different functions: function1 and function2. Even the number of parameters in each function is different. There's no ambiguity here whatsoever. When you ask the compiler to call function1(10) it calls function1(10, 3.1). function2 doesn't even come into the picture.

If it were the same function, then the issue of ambiguity would not arise simply because it is illegal in C++ to specify a default argument for the same parameter more than once (within the same translation unit). Even of you specify the same default argument value the second time, the program is ill-formed

void foo(int a = 5);
void foo(int a = 5); // <- ERROR

What one can do though is to specify a different set of default arguments for the same function in different translation units. But that does not create any ambiguity because the compiler can see only one translation unit. The compiler simply will never know of any potential "ambiguity" is that case.

AndreyT
What if it were two different functions with the same function name? I think that's what the OP really meant.
David Thornley
A: 

Furthermore, the answer to any question starting with, "What does the C++ compiler do...," is always going to be, "Depends on which compiler you're talking about."

Noah Roberts
Okay, let's say it was the GNU compiler.
Which version? ....filler characters...
Noah Roberts
+8  A: 

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.

Johannes Schaub - litb
Is this void function(int a = 0, float b); really ok? Are you sure?
Max