【问题标题】:gcc and clang differs in behavior when calling variadic member function template in a variadic class template在可变参数类模板中调用可变参数成员函数模板时,gcc 和 clang 的行为不同
【发布时间】:2021-08-18 03:21:33
【问题描述】:

以下代码

#include <iostream>

template <class... Ts>
struct A
{
    template <Ts ...Args>
    static void f() {
        (std::cout << ... << Args) << std::endl;
    }
};

int main() {
    A<int, int, int, int>::f<0, 1, 2, 3>();
}

使用-std=c++17,使用clang 12.0.1 编译并打印0123,但使用g++ 11.1 编译失败,出现以下错误:

test.cpp: In function ‘int main()’:
test.cpp:13:41: error: no matching function for call to ‘A<int, int, int, int>::f<0, 1, 2, 3>()’
   13 |     A<int, int, int, int>::f<0, 1, 2, 3>();
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
test.cpp:7:17: note: candidate: ‘template<Ts ...Args> static void A<Ts>::f() [with Ts ...Args = {Args ...}; Ts = {int, int, int, int}]’
    7 |     static void f() {
      |                 ^
test.cpp:7:17: note:   template argument deduction/substitution failed:
test.cpp:13:41: error: wrong number of template arguments (4, should be 1)
   13 |     A<int, int, int, int>::f<0, 1, 2, 3>();
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
test.cpp:7:17: note: provided for ‘template<Ts ...Args> static void A<Ts>::f() [with Ts ...Args = {Args ...}; Ts = {int, int, int, int}]’
    7 |     static void f() {
      |                 ^

正确的行为是什么?

【问题讨论】:

标签: c++ language-lawyer variadic-templates


【解决方案1】:

这显然是一个 GCC 错误。有几个错误报告,例如,777318858091247。它将在 GCC 12 中修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-18
    • 2021-10-01
    • 2016-10-05
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    相关资源
    最近更新 更多