如何将模板参数包扩展到一系列模板化参数
how to expand template parameter pack to series of templated arguments?
我有这个简单的模板类:
template<typename T>
class property
{
// ...
};
而这个可变参数模板类:
template<typename... factory_args>
class type_t
{
// …
在此类中,我希望有一个构造函数可以扩展为:
type_t (property<first_type>&, property<second_type>& etc.)
first_type
、second_type
等应该来自factory_args
。
然后,我将能够像这样调用构造函数:
property<int> first = etc...;
property<void*> second = ...;
auto some_type = type_t<int, void*>(first, second);
理想情况下,模板参数推导也可以工作,所以我也可以这样称呼它:
auto some_other_type = type_t(first, second);
如何编写type_t
构造函数?这在 C++17 中可行吗?
像这样:
template<typename... factory_args>
class type_t
{
public:
type_t(property<factory_args>&... args);
};
类模板参数推导在这里也做了正确的事情。因此,如果您有:
property<int> i;
property<void*> v;
type_t x(i, v); // ok, x is a type_t<int, void*>
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 函数获取参数的变量列表并将它们转换为一系列字节?
- 如何将模板参数包扩展到一系列模板化参数
- 实现一个从输入流中读取一系列值作为参数的函数