You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

带约束的函数重载解析:调用f()是否存在歧义?

调用函数模板f()是否存在歧义?

我认为调用f()不应存在歧义,因为其中一个函数的约束更强。但GCC 12.2和Clang trunk拒绝编译这段代码,而Clang 15、MSVC 19.33和MSVC 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;
      |     ^

内容的提问来源于stack exchange,提问作者303

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 14:55:13