【发布时间】: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