【发布时间】:2017-02-22 05:39:08
【问题描述】:
假设我有一个可变参数模板类。如何创建一个函数,使其参数为集合类型,例如int,参数的数量等于模板类型的数量?
template <typename... Types>
class Test
{
public:
void Func(???); // I don't know how to declare such a function
}
Test<string, bool, long> myTest; // Three types
myTest.Func(905, 36, 123315); // Three arguments, but always of type int.
最后,函数的目标是返回一个由所提供的整数组成的元组。为简单起见,我在示例代码中显示该函数为 void。
【问题讨论】:
-
This 和额外的
sizeof...check? -
喜欢这个? ideone.com/PASclP
-
这里甚至不需要
Types,因为参数包的大小可以通过调用来推断:ideone.com/tPe6Tj -
@Simon Kraemer 我希望 Func 的参数数量等于类模板类型的数量。此外,我想强制它们始终为整数。
-
@Gill Bates 这已经非常接近了。接下来对我来说:如何让返回类型 std::tuple 包含等于类型数量的整数?
标签: c++ c++11 templates constructor variadic-templates