如何将具有不同模板参数的相同结构作为参数包传递

How pass the same struct with different template argument as parameter pack

本文关键字:参数 包传递 结构      更新时间:2023-10-16

感谢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 << ")";
}