解开variadic参数并相应地传递元素

发布于 2025-01-27 11:05:56 字数 1007 浏览 2 评论 0原文

假设我得到了一个结构IE coord包含两个静态成员变量,然后将其作为variadic Template函数的参数传递给它variadic_print_coord(),我如何解开variadic表达式,调用print_pair()函数,如下所示。

template<class T1, class T2>
void print_pair(T1 t1, T2 t2)
{
  std::cout << t1 << " and " << t2 << '\n';
}

template<class T1, class T2, class ...Ts>
void print_pair(T1 t1, T2 t2, Ts... ts)
{
  print_pair(t1, t2);
  print_pair(ts... );
}

template<int X, int Y>
struct Coord
{
  static const int valueX = X;
  static const int valueY = Y;
}

template<class... COORDs>
void variadic_print_coord()
{
  print_pair(COORD1::valueX, COORD1::valueY, COORD2::valueX, COORD2::valueY, ...,
             COORDs::valueX, COORDs::valueY);
  //how could I manipulating the pack expressions to get something like this 
}

int main()
{
  print_pair<Coord<0,1>, Coord<2,3>, Coord<4,5>>();
  //result:
  //0 and 1
  //2 and 3
  //4 and 5
}


非常感谢!

suppose I got a struct i.e. Coord that contains two static member variables, then pass it as an argument of variadic template function variadic_print_coord(), how do I unpack the variadic expressions, to call the print_pair() function that shown below.

template<class T1, class T2>
void print_pair(T1 t1, T2 t2)
{
  std::cout << t1 << " and " << t2 << '\n';
}

template<class T1, class T2, class ...Ts>
void print_pair(T1 t1, T2 t2, Ts... ts)
{
  print_pair(t1, t2);
  print_pair(ts... );
}

template<int X, int Y>
struct Coord
{
  static const int valueX = X;
  static const int valueY = Y;
}

template<class... COORDs>
void variadic_print_coord()
{
  print_pair(COORD1::valueX, COORD1::valueY, COORD2::valueX, COORD2::valueY, ...,
             COORDs::valueX, COORDs::valueY);
  //how could I manipulating the pack expressions to get something like this 
}

int main()
{
  print_pair<Coord<0,1>, Coord<2,3>, Coord<4,5>>();
  //result:
  //0 and 1
  //2 and 3
  //4 and 5
}


many thanks!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜空下最亮的亮点 2025-02-03 11:05:56

在这种情况下,您可以使用涉及折叠表达式的以下构造

template<class... COORDs>
void variadic_print_coord()
{
  (print_pair(COORDs::X, COORDs::Y), ...);
}

,您不需要print_pair的variadic版本,因为调用基本上是截断。


#include <iostream>

template<class T1, class T2>
void print_pair(T1 t1, T2 t2)
{
  std::cout << t1 << " and " << t2 << '\n';
}

template<int X, int Y>
struct Coord
{
  static const int valueX = X;
  static const int valueY = Y;
};

template<class... COORDs>
void variadic_print_coord()
{
  (print_pair(COORDs::valueX, COORDs::valueY), ...);
}

int main()
{
  variadic_print_coord<Coord<0,1>, Coord<2,3>, Coord<4,5>>();
  //result:
  //0 and 1
  //2 and 3
  //4 and 5
}

You can use the following construct involving a fold expression

template<class... COORDs>
void variadic_print_coord()
{
  (print_pair(COORDs::X, COORDs::Y), ...);
}

In this case you won't need the variadic version of print_pair, as the calls basically decouple.


#include <iostream>

template<class T1, class T2>
void print_pair(T1 t1, T2 t2)
{
  std::cout << t1 << " and " << t2 << '\n';
}

template<int X, int Y>
struct Coord
{
  static const int valueX = X;
  static const int valueY = Y;
};

template<class... COORDs>
void variadic_print_coord()
{
  (print_pair(COORDs::valueX, COORDs::valueY), ...);
}

int main()
{
  variadic_print_coord<Coord<0,1>, Coord<2,3>, Coord<4,5>>();
  //result:
  //0 and 1
  //2 and 3
  //4 and 5
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文