如何将具有不同模板参数的相同结构作为参数包传递
How pass the same struct with different template argument as parameter pack
感谢SO伙计们,我解决了我的一个问题:创建具有可变类型包装的元组
但在那之后我意识到,我仍然有一个我无法解决的问题。所以现在我有:
template < typename T,
size_t Size >
struct Metadata {
using type = T;
std::bitset<Size> bitset;
};
template <class... Ts>
constexpr auto make_metadata()
{
constexpr size_t N = sizeof...(Ts);
return std::make_tuple(Metadata<Ts, N>{0}...);
}
我打算使用它"喜欢":
constexpr auto result = make_metadata<Foo1, Foo2, Foo3>({0}, {0}, {0});
根据 Jarod42 的评论,我想我需要 2 个功能。
但是我如何将参数传递给函数,然后传递给元组呢?
我想知道如何做到这一点,但没有强制为每个 T 传递每个参数,如果它们不存在,我只会输入一个默认值(2 个问题)。
这可能是
你想要的,我不确定。(因为您从不显示要使用的参数的位置)
#include <tuple>
#include <bitset>
#include <iostream>
struct A{int value;};
struct B{int value;};
struct C{int value;};
template <typename T,int Size>
struct Metadata{
using type = T;
T value;
std::bitset<Size> bitset;
};
template <typename...Ts,typename...Args>
constexpr auto make_metadata(Args... args)
{
constexpr auto N = sizeof...(Ts);
return std::make_tuple(Metadata<Ts, N>{args,0}...);
}
int main(){
auto data = make_metadata<A,B,C>(1,2,3);
std::cout << "(" << std::get<0>(data).value.value
<< ", " << std::get<1>(data).value.value
<< ", " << std::get<2>(data).value.value << ")";
}
相关文章:
- 如何反转整数参数包
- 如何将enable-if与模板参数和参数包一起使用
- 模板元编程:如何将参数包组合成新的参数包
- C ++:在构造函数中使用参数包?
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 双模板参数包
- 参数包构造函数在类模板中隐藏用户定义的转换
- 嵌套参数包扩展失败
- 将指针传递到成员的指针,从模板参数包到函数对象
- 如何将将参数包作为参数传递给其他模板类的模板类?
- 当模板类使用参数包时,如何传递其他模板参数
- 将函数应用于元组中的每个元素,将每个元素强制转换为类型包中的不同类型,然后作为参数包传递
- 传递显式空参数包
- 当将参数包传递给另一个可变参数模板函数时,我是否必须使用 std::forward
- 是否可以调用具有不同参数数量的函数,传递相同的参数包
- 如何在C++中传递参数包
- 如何将具有不同模板参数的相同结构作为参数包传递
- 将选定数量的参数包传递/转发给另一个函数
- 如何通过参数包传递引用
- 参数包传递问题