简体   繁体   English

约束函数的重载解析

[英]Overload resolution of constrained functions

Is the call to f ambiguous?f的调用不明确吗? Since one of the functions is more constrained than the other, I wouldn't expect there to be any ambiguity.由于其中一个功能比另一个更受限制,我不希望有任何歧义。 Yet, GCC 12.2 and Clang trunk reject the code, in contrast to Clang 15 , MSVC 19.33 , and MSVC trunk .然而, GCC 12.2Clang trunk拒绝代码,与Clang 15MSVC 19.33MSVC trunk形成对比。

Code example :代码示例

template<typename...>
int f();

template<typename = void>
int f() requires true;

static_assert(sizeof(f()));

The error produced by GCC: GCC产生的错误:

<source>:7:23: error: call of overloaded 'f()' is ambiguous
    7 | static_assert(sizeof(f()));
      |                      ~^~
<source>:2:5: note: candidate: 'int f() [with <template-parameter-1-1> = {}]'
    2 | int f();
      |     ^
<source>:5:5: note: candidate: 'int f() requires  true [with <template-parameter-1-1> = void]'
    5 | int f() requires true;
      |     ^

Whether viable overloads from function templates are more constrained is considered only if the function templates are otherwise equivalent (except for potentially the return type).仅当 function 模板在其他方面是等效的(潜在的返回类型除外)时,才会考虑来自 function 模板的可行重载是否受到更多约束。 It is not sufficient that the call would otherwise be ambiguous.否则呼叫会模棱两可是不够的。

In particular the corresponding template parameters must be equivalent , see[temp.func.order]/6.2.2 .特别是相应的模板参数必须等效,请参阅[temp.func.order]/6.2.2 A template parameter that is a parameter pack (as in the first overload) is not equivalent to one that isn't (as in the second overload), see [temp.over.link]/6.2 .作为参数包的模板参数(如在第一个重载中)不等同于不是参数包的模板参数(如在第二个重载中),请参阅[temp.over.link]/6.2

So the constraint doesn't matter for deciding which candidate is better.因此,约束对于决定哪个候选人更好并不重要。 The call is ambiguous.这个电话是模棱两可的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM