了解 C++ 可变数量的输入参数到 test_function(Test<T>...)
Understanding C++ variable number of input parameters to test_function(Test<T>...)
我有一个通用的 class Test<T>
并且我想要一个函数 test_function()
具有可变数量的 Test<T>
对象输入参数 ...
.在函数中,我想遍历所有参数。通用类型 T
可以在参数之间不同。像这样:
template <typename T> class Test {
private:
T value = (T)0;
int test = 1;
public:
Test() = default;
int get_test() {
return test;
}
}
template <typename T> void test_function(const Test<T> tests...) {
for(auto test : tests) {
cout << test.get_test() << endl;
}
}
编译时出现错误:
error C3520: "T": Parameter pack must be extended in this context
error C3520: "tests": Parameter pack must be extended in this context
error C3312: no callable 'begin' function found for type 'unknown-type'
error C3312: no callable 'end' function found for type 'unknown-type'
我做错了什么?
编辑:是否可以在资料片中加入一个计数器?
编辑 2:我用计数器计算出来了:
template <typename ...T> void test_function(const Test<T> ...tests) {
int i=0;
((cout << tests.get_test() << ", counter = " << i++ << endl), ...);
}
这里有多个问题。
首先,正确的可变模板函数声明应该是:
template <typename ...T> void test_function(const Test<T> ...tests)
但这并不能解决所有问题。第一个是所有参数都是 const
对象,因此 class 方法也必须是一个 const class 成员:
int get_test() const {
最后:
for(auto test : tests) {
tests
不是可以像向量一样进行范围迭代的容器。是一个参数包,需要像一个一样展开:
((cout << tests.get_test()), ...);
使用 gcc 11 测试:
#include <iostream>
using namespace std;
template <typename T> class Test {
private:
T value = (T)0;
int test = 1;
public:
Test() = default;
int get_test() const {
return test;
}
};
template <typename ...T> void test_function(const Test<T> ...tests) {
((cout << tests.get_test()), ...);
}
void foo()
{
test_function(Test<int>{}, Test<char>{});
}
我有一个通用的 class Test<T>
并且我想要一个函数 test_function()
具有可变数量的 Test<T>
对象输入参数 ...
.在函数中,我想遍历所有参数。通用类型 T
可以在参数之间不同。像这样:
template <typename T> class Test {
private:
T value = (T)0;
int test = 1;
public:
Test() = default;
int get_test() {
return test;
}
}
template <typename T> void test_function(const Test<T> tests...) {
for(auto test : tests) {
cout << test.get_test() << endl;
}
}
编译时出现错误:
error C3520: "T": Parameter pack must be extended in this context
error C3520: "tests": Parameter pack must be extended in this context
error C3312: no callable 'begin' function found for type 'unknown-type'
error C3312: no callable 'end' function found for type 'unknown-type'
我做错了什么?
编辑:是否可以在资料片中加入一个计数器?
编辑 2:我用计数器计算出来了:
template <typename ...T> void test_function(const Test<T> ...tests) {
int i=0;
((cout << tests.get_test() << ", counter = " << i++ << endl), ...);
}
这里有多个问题。
首先,正确的可变模板函数声明应该是:
template <typename ...T> void test_function(const Test<T> ...tests)
但这并不能解决所有问题。第一个是所有参数都是 const
对象,因此 class 方法也必须是一个 const class 成员:
int get_test() const {
最后:
for(auto test : tests) {
tests
不是可以像向量一样进行范围迭代的容器。是一个参数包,需要像一个一样展开:
((cout << tests.get_test()), ...);
使用 gcc 11 测试:
#include <iostream>
using namespace std;
template <typename T> class Test {
private:
T value = (T)0;
int test = 1;
public:
Test() = default;
int get_test() const {
return test;
}
};
template <typename ...T> void test_function(const Test<T> ...tests) {
((cout << tests.get_test()), ...);
}
void foo()
{
test_function(Test<int>{}, Test<char>{});
}