我有一个tuple
,pair
其中每对都有固定类型和变体类型。我想要的是变量类型的列表或使用这些类型定义一个对象(肯定是使用可变模板包构造的)。
根据我如何将tuple<>
背面剥离为可变参数模板类型列表?,可以使用的构成类型实例化某些内容tuple
。但是在这种情况下,我不知道这是否可能,以及如何做到。
想象一下,我有这样的事情:
struct MyFixedType
{ /* ... */ };
using Tuple = std::tuple<
std::pair<MyFixedType, int>,
std::pair<MyFixedType, double>,
std::pair<MyFixedType, std::string>
>;
我有这个课:
template <typename... Ts>
class MyClass
{
};
我想要的是声明MyClass
with的对象<int, double, std::string>
注意:尽管可以使用预处理器或某些宏来处理这种情况,但不允许使用宏。
为了简洁起见,使用函数模板参数推导:
template <class Fixed, class... Variant>
MyClass<Variant...> MyClassFromPairTuple_impl(std::tuple<std::pair<Fixed, Variant>...> &&);
template <class Tuple>
using MyClassFromPairTuple = decltype(MyClassFromPairTuple_impl(std::declval<Tuple>()));
用法:
using Tuple = std::tuple<std::pair<MyFixedType, int>, std::pair<MyFixedType, double>, std::pair<MyFixedType, std::string>>;
MyClassFromPairTuple<Tuple> instance; // MyClass<int, double, std::string>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句