GCC不允许把函数指针作为实参传递给带有const参数的变参函数模板参数

编程语言 2026-07-09

我写了下面这个程序,clang和 MSVC能通过,但GCC会拒绝它。

示例

template<typename... T>
double BigFunction(double (*func)(const T...), double var)
{
    return {};
}

double f3(double, int)
{
    return {};
}

int main()
{ 
    BigFunction(f3, 3);// GCC: no but Clang + MSVC: ok
}

GCC的说法是:

<source>: In function 'int main()':
<source>:15:16: error: no matching function for call to 'BigFunction(double (&)(double, int), int)'
   15 |     BigFunction(f3, 3);// GCC: no but Clang + MSVC: ok
      |     ~~~~~~~~~~~^~~~~~~
  • there is 1 candidate
    • candidate 1: 'template<class ... T> double BigFunction(double (*)(const T ...), double)'
      <source>:3:32:
          3 | template<typename... T> double BigFunction(double (*func)(const T...), double var)
            |                                ^~~~~~~~~~~
      • template argument deduction/substitution failed:
        •   types 'const T' and 'double' have incompatible cv-qualifiers
          <source>:15:16:
             15 |     BigFunction(f3, 3);// GCC: no but Clang + MSVC: ok
                |     ~~~~~~~~~~~^~~~~~~
ASM generation compiler returned: 1
<source>: In function 'int main()':
<source>:15:16: error: no matching function for call to 'BigFunction(double (&)(double, int), int)'
   15 |     BigFunction(f3, 3);// GCC: no but Clang + MSVC: ok
      |     ~~~~~~~~~~~^~~~~~~
  • there is 1 candidate
    • candidate 1: 'template<class ... T> double BigFunction(double (*)(const T ...), double)'
      <source>:3:32:
          3 | template<typename... T> double BigFunction(double (*func)(const T...), double var)
            |                                ^~~~~~~~~~~
      • template argument deduction/substitution failed:
        •   types 'const T' and 'double' have incompatible cv-qualifiers
          <source>:15:16:
             15 |     BigFunction(f3, 3);// GCC: no but Clang + MSVC: ok
                |     ~~~~~~~~~~~^~~~~~~

按照最新的C++标准,我想知道这里到底哪个编译器才是正确的。

解决方案

我想知道在这里到底应该使用哪个编译器才是正确的。

这是一个已确认的 gcc bug,在gcc的缺陷跟踪系统中被标记为一个 回归(regression)。也许会有其他人提供来自标准的确切引用来补充本答案。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章