用于打印参数包内容的递归可变参数模板

发布于 2024-11-30 17:34:34 字数 294 浏览 2 评论 0原文

如何创建递归可变参数模板来打印参数包的内容? 我正在尝试这样做,但它无法编译:

template <typename First, typename ...Args>
std::string type_name () {
    return std::string(typeid(First).name()) + " " + type_name<Args...>();
}
std::string type_name () {
    return "";
}

我该如何结束递归?

How is it possible to create a recursive variadic template to print out the contents of a paramater pack?
I am trying with this, but it fails to compile:

template <typename First, typename ...Args>
std::string type_name () {
    return std::string(typeid(First).name()) + " " + type_name<Args...>();
}
std::string type_name () {
    return "";
}

How shall I end the recursion?

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

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

发布评论

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

评论(6

—━☆沉默づ 2024-12-07 17:34:34

实际上有一种非常优雅的方式来结束递归:

template <typename Last>
std::string type_name () {
    return std::string(typeid(Last).name());
}

template <typename First, typename Second, typename ...Rest>
std::string type_name () {
    return std::string(typeid(First).name()) + " " + type_name<Second, Rest...>();
}

我最初尝试了 templatetemplate 但那是被认为是不明确的(其余可以是零元素)。然后这个问题向我展示了最终的解决方案: Compilation Error on Recursive Variadic Template Function


注意,为了避免一些代码重复,您还可以这样做:

template <typename Last>
std::string type_name () {
    return std::string(typeid(Last).name());
}

template <typename First, typename Second, typename ...Rest>
std::string type_name () {
    return type_name<First>() + " " + type_name<Second, Rest...>();
}

There's actually a very elegant way to end the recursion:

template <typename Last>
std::string type_name () {
    return std::string(typeid(Last).name());
}

template <typename First, typename Second, typename ...Rest>
std::string type_name () {
    return std::string(typeid(First).name()) + " " + type_name<Second, Rest...>();
}

I initially tried template <typename Last> and template <typename First, typename ...Rest> but that was considered ambiguous (Rest can be zero elements). This question then showed me the definitive solution: Compilation Error on Recursive Variadic Template Function


Note, to avoid a bit of code duplication, you could also do:

template <typename Last>
std::string type_name () {
    return std::string(typeid(Last).name());
}

template <typename First, typename Second, typename ...Rest>
std::string type_name () {
    return type_name<First>() + " " + type_name<Second, Rest...>();
}
英雄似剑 2024-12-07 17:34:34

您需要使用部分特化来结束递归,但由于您无法部分特化 C++ 中的自由函数,因此您需要创建一个带有静态成员函数的实现类。

template <typename... Args>
struct Impl;

template <typename First, typename... Args>
struct Impl<First, Args...>
{
  static std::string name()
  {
    return std::string(typeid(First).name()) + " " + Impl<Args...>::name();
  }
};

template <>
struct Impl<>
{
  static std::string name()
  {
    return "";
  }
};

template <typename... Args>
std::string type_name()
{
    return Impl<Args...>::name();
}

int main()
{
  std::cout << type_name<int, bool, char, double>() << std::endl; // "i b c d"
  return 0;
}

Impl 的第一个声明只是 g++ 4.6(及更低版本)中的缺陷的解决方法。一旦正确实现了可变参数模板,就没有必要了。

在 ideone.com 上查看实际情况

You need to use partial specialisation to end the recursion, but since you can't partially specialise free functions in C++, you need to create an implementation class with a static member function.

template <typename... Args>
struct Impl;

template <typename First, typename... Args>
struct Impl<First, Args...>
{
  static std::string name()
  {
    return std::string(typeid(First).name()) + " " + Impl<Args...>::name();
  }
};

template <>
struct Impl<>
{
  static std::string name()
  {
    return "";
  }
};

template <typename... Args>
std::string type_name()
{
    return Impl<Args...>::name();
}

int main()
{
  std::cout << type_name<int, bool, char, double>() << std::endl; // "i b c d"
  return 0;
}

That first declaration of Impl is just a workaround for a shortcoming in g++ 4.6 (and below). It won't be necessary once it implements variadic templates correctly.

Check it out in action at ideone.com

镜花水月 2024-12-07 17:34:34

C++17 的 if constexpr 允许您在一个模板声明中执行此操作,与许多旧的解决方案不同,它非常容易理解:

template <typename T, typename ...Args>
std::string type_name() {
  if constexpr (!sizeof...(Args)) {
    return std::string(typeid(T).name());
  } else {
    return std::string(typeid(T).name()) + " " + type_name<Args...>();
  }
}

C++17's if constexpr allows you to do this in one template declaration which is, unlike a lot of the older solutions, pretty easy to understand:

template <typename T, typename ...Args>
std::string type_name() {
  if constexpr (!sizeof...(Args)) {
    return std::string(typeid(T).name());
  } else {
    return std::string(typeid(T).name()) + " " + type_name<Args...>();
  }
}
我做我的改变 2024-12-07 17:34:34

作为不存在的函数部分特化的替代方法,您可以在类型化器类上使用重载:

#include <string>
#include <iostream>
#include <typeinfo>

template <unsigned int N> struct NumberToType { };

template <typename T>
std::string my_type_name(NumberToType<0> = NumberToType<0>())
{
  return std::string(typeid(T).name());
}

template <typename T, typename ...Args>
std::string my_type_name(NumberToType<sizeof...(Args)> = NumberToType<sizeof...(Args)>())
{
  return std::string(typeid(T).name()) + " " + my_type_name<Args...>(NumberToType<sizeof...(Args)-1>());
}

int main()
{
  std::cout << my_type_name<int, double, char>() << std::endl;
}

As an alternative to non-existing partial specialization for functions, you can use overloading on a typifier class:

#include <string>
#include <iostream>
#include <typeinfo>

template <unsigned int N> struct NumberToType { };

template <typename T>
std::string my_type_name(NumberToType<0> = NumberToType<0>())
{
  return std::string(typeid(T).name());
}

template <typename T, typename ...Args>
std::string my_type_name(NumberToType<sizeof...(Args)> = NumberToType<sizeof...(Args)>())
{
  return std::string(typeid(T).name()) + " " + my_type_name<Args...>(NumberToType<sizeof...(Args)-1>());
}

int main()
{
  std::cout << my_type_name<int, double, char>() << std::endl;
}
岛徒 2024-12-07 17:34:34

作为替代方案,您可以就地解压参数包,如下例所示:

#include<string>
#include<iostream>
#include<typeinfo>

template <typename T, typename ...Args>
std::string type_name () {
    std::string str = typeid(T).name();
    int arr[] = { 0, (str += std::string{" "} + typeid(Args).name(), 0)... };
    (void)arr;
    return str;
}

int main() {
    auto str = type_name<int, double, char>();
    std::cout << str << std::endl;
}

实际上不需要递归来执行此操作。

As an alternative, you can unpack the parameter pack in-place as in the following example:

#include<string>
#include<iostream>
#include<typeinfo>

template <typename T, typename ...Args>
std::string type_name () {
    std::string str = typeid(T).name();
    int arr[] = { 0, (str += std::string{" "} + typeid(Args).name(), 0)... };
    (void)arr;
    return str;
}

int main() {
    auto str = type_name<int, double, char>();
    std::cout << str << std::endl;
}

Recursion is not required actually to do that.

笑着哭最痛 2024-12-07 17:34:34

使用C++17折叠表达式:

template <typename ...Args>
std::string type_name () {
    return (std::string(typeid(Args).name()) + " " + ...);
}

Use C++17 fold expression:

template <typename ...Args>
std::string type_name () {
    return (std::string(typeid(Args).name()) + " " + ...);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文