C++ - 如何通过变异模板访问向量推入矢量的元素

发布于 2025-02-09 18:23:04 字数 824 浏览 1 评论 0原文

我需要像这样调用C类对象 -

int main()
{
  C<Pair<"x", A<int>>, Pair<"y", A<double>>, Pair<"z", B<int>>> c1;
  C<Pair<"x", A<int>>, Pair<"y", A<double>>> c2;
}

到目前为止我有此代码 -

template <typename X>
class A
{
  .....
};

template <typename Y>
class B
{
  .....
};

template <typename S, typename T>
struct Pair // wrap a generic type
{
  pair<S,T> p;

}

template <typename... P>
class C
{
  tuple<Pair*...> v; // every time the class and type will differ??
  public:

  void func()
  {
    v.push_back(//all P in sequence);
    //access the 1st and 2nd element of each pair in vector
  }
  
};

如何按顺序推动所有P,然后访问C类中每个对的元素?

如果上述方法有故障,那么我可以使用哪种其他方式来调用C类,如上所述?

I need to call the C class object like this -

int main()
{
  C<Pair<"x", A<int>>, Pair<"y", A<double>>, Pair<"z", B<int>>> c1;
  C<Pair<"x", A<int>>, Pair<"y", A<double>>> c2;
}

I have this code so far -

template <typename X>
class A
{
  .....
};

template <typename Y>
class B
{
  .....
};

template <typename S, typename T>
struct Pair // wrap a generic type
{
  pair<S,T> p;

}

template <typename... P>
class C
{
  tuple<Pair*...> v; // every time the class and type will differ??
  public:

  void func()
  {
    v.push_back(//all P in sequence);
    //access the 1st and 2nd element of each pair in vector
  }
  
};

How can I push all P in sequence and then access the elements of each pair in class C?

If there is a fault in the above approach, then what other way I can use to be able call class C object like mentioned above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文