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

后端 未结 4 445
予麋鹿
予麋鹿 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:23

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

提交回复
热议问题