首页 > 解决方案 > 将可变参数包的每个元素包装在包装器中并存储在 std::tuple 中

问题描述

考虑两个类:

struct A {};
struct B {};

还有一个类:

template<typename... Ts>
struct packed {
   using Tuple = std::tuple<Ts...>;
   Tuple tpl;
};

template<typename T>
struct wrapper {
T t;
};

我想要的不是Tuple = std::tuple<Ts...>inside ofpacked而是一个元组,它的每个元素都像using WrappedTuple = std::tuple<wrapper<T0>, wrapper<T1>, ..., wrapper<TN-1>>;

目标是通过一组通用属性来增强每个给定类型。

标签: c++c++17stdtuple

解决方案


只需wrapper<Ts>扩展.... 这会生成一个模板参数包,其中每个元素是wrapper<T>T是包中的每个相应类型Ts

using Tuple = std::tuple<wrapper<Ts>...>;

演示


推荐阅读