C++ 是什么意思?当出现不明确的默认参数时编译器会做什么?
当出现不明确的默认参数时,C++ 编译器会做什么?例如,假设有一个函数,例如:
void function(int a = 0, float b = 3.1);
void function(int a, float b =1.1, int c = 0);
上面的内容是否被认为是不明确的?如果不是,编译器在调用类似 function1(10)
时会做什么(函数如何精确匹配)?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下内容很好
,以下内容也很好
,但以下内容不明确
对于重载解析(告诉调用哪个函数的过程),未传递显式参数和使用默认参数的参数将被忽略。因此,编译器确实看到两个函数都具有用于上述调用的一个 int 参数,但无法做出决定。
尽管以下内容格式不正确,
但对于问题中的代码,您声明了两个函数(因为两个声明都有不同数量的参数),但在本例中您只声明了一个功能。但它的第二个声明重复了参数的默认参数(甚至使用不同的值,但这不再重要)。这是不允许的。请注意,以下情况可以
合并出现在同一函数的同一作用域中的声明的默认参数集,并且仅适用于出现在同一作用域中的声明。因此以下内容是有效的。
这将调用为
b
传递1.1
的函数。The following is fine
And the following is fine too
But the following is ambiguous
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
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
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
This calls the function with
1.1
passed forb
.如果它们有不同的名称(如您的示例中所示),则不会产生歧义。如果它们具有相同的名称(因此这是一次重载的尝试),编译器会抱怨。
虽然事实证明你可以在不同的作用域中重新定义函数的默认参数(这对我来说是新闻......) - 但在相同的作用域中,你不能将默认参数重新定义即使是相同的值< /em>.来自 8.3.6/4“默认参数”:
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":
模糊的?您有两个完全独立的不同函数:
function1
和function2
。甚至每个函数中的参数数量也是不同的。这里没有任何歧义。当您要求编译器调用function1(10)
时,它会调用function1(10, 3.1)
。function2
甚至没有出现在图中。如果它是同一个函数,那么就不会出现歧义问题,因为在 C++ 中为同一参数多次指定默认参数(在同一翻译单元内)是非法的。即使您第二次指定相同的默认参数值,程序也是不正确的。
但是我们可以做的是在不同的翻译中为同一函数指定一组不同的默认参数单位。但这不会产生任何歧义,因为编译器只能看到一个翻译单元。在这种情况下,编译器永远不会知道任何潜在的“歧义”。
Ambiguous? You have two completely independent different functions:
function1
andfunction2
. Even the number of parameters in each function is different. There's no ambiguity here whatsoever. When you ask the compiler to callfunction1(10)
it callsfunction1(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
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.
此外,任何以“C++ 编译器做什么……”开头的问题的答案始终是“取决于您正在谈论的编译器”。
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."