gcc 和 clang 在可变 class 模板中调用可变成员函数模板时的行为不同
gcc and clang differs in behavior when calling variadic member function template in a variadic class template
以下代码
#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
,使用 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() {
| ^
哪种行为是正确的?
以下代码
#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
,使用 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() {
| ^
哪种行为是正确的?