Search code examples
c++language-lawyervariadic-templates

gcc and clang differs in behavior when calling variadic member function template in a variadic class template


The following code

#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>();
}

with -std=c++17, compiles with clang 12.0.1 and prints 0123, but fails to compile with g++ 11.1 with the following error:

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() {
      |                 ^

Which is the correct behavior?


Solution

  • This is clearly a GCC bug. There are several bug reports, for example, 77731, 88580 and 91247. It will be fixed in GCC 12.