C++ 是什么意思?当出现不明确的默认参数时编译器会做什么?

发布于 2024-09-26 01:22:39 字数 254 浏览 0 评论 0原文

当出现不明确的默认参数时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情仇皆在手 2024-10-03 01:22:39

以下内容很好

void function(int a = 0, float b = 3.1);
void function(int a, float b =1.1, int c = 0);

,以下内容也很好

function(); // calls first function

,但以下内容不明确

function(1); // second and first match equally well

对于重载解析(告诉调用哪个函数的过程),未传递显式参数和使用默认参数的参数将被忽略。因此,编译器确实看到两个函数都具有用于上述调用的一个 int 参数,但无法做出决定。

尽管以下内容格式不正确,

void function(int a = 0, float b = 3.1);
void function(int a, float b =1.1);

但对于问题中的代码,您声明了两个函数(因为两个声明都有不同数量的参数),但在本例中您只声明了一个功能。但它的第二个声明重复了参数的默认参数(甚至使用不同的值,但这不再重要)。这是不允许的。请注意,以下情况可以

void function(int a, float b = 3.1);
void function(int a = 0, float b);

合并出现在同一函数的同一作用域中的声明的默认参数集,并且仅适用于出现在同一作用域中的声明。因此以下内容是有效的。

void function(int a = 0, float b = 3.1);
void function1() {
  void function(int a, float b = 1.1); 
  function(0);
}

这将调用为 b 传递 1.1 的函数。

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.

夜司空 2024-10-03 01:22:39

如果它们有不同的名称(如您的示例中所示),则不会产生歧义。如果它们具有相同的名称(因此这是一次重载的尝试),编译器会抱怨。

虽然事实证明你可以在不同的作用域中重新定义函数的默认参数(这对我来说是新闻......) - 但在相同的作用域中,你不能将默认参数重新定义即使是相同的值< /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":

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

遗弃M 2024-10-03 01:22:39

模糊的?您有两个完全独立的不同函数:function1function2。甚至每个函数中的参数数量也是不同的。这里没有任何歧义。当您要求编译器调用 function1(10) 时,它会调用 function1(10, 3.1)function2 甚至没有出现在图中。

如果它是同一个函数,那么就不会出现歧义问题,因为在 C++ 中为同一参数多次指定默认参数(在同一翻译单元内)是非法的。即使您第二次指定相同的默认参数值,程序也是不正确的。

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

但是我们可以做的是在不同的翻译中为同一函数指定一组不同的默认参数单位。但这不会产生任何歧义,因为编译器只能看到一个翻译单元。在这种情况下,编译器永远不会知道任何潜在的“歧义”。

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.

卸妝后依然美 2024-10-03 01:22:39

此外,任何以“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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文