繁体   English   中英

约束函数的重载解析

[英]Overload resolution of constrained functions

f的调用不明确吗? 由于其中一个功能比另一个更受限制,我不希望有任何歧义。 然而, GCC 12.2Clang trunk拒绝代码,与Clang 15MSVC 19.33MSVC trunk形成对比。

代码示例

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

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

static_assert(sizeof(f()));

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;
      |     ^

仅当 function 模板在其他方面是等效的(潜在的返回类型除外)时,才会考虑来自 function 模板的可行重载是否受到更多约束。 否则呼叫会模棱两可是不够的。

特别是相应的模板参数必须等效,请参阅[temp.func.order]/6.2.2 作为参数包的模板参数(如在第一个重载中)不等同于不是参数包的模板参数(如在第二个重载中),请参阅[temp.over.link]/6.2

因此,约束对于决定哪个候选人更好并不重要。 这个电话是模棱两可的。

暂无
暂无

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

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