What does the C++ compiler do when coming ambiguous default parameters?

后端 未结 4 468
予麋鹿
予麋鹿 2021-01-13 01:19

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          


        
4条回答
  •  自闭症患者
    2021-01-13 02:14

    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.

提交回复
热议问题