我有一个打字员提供了以下界面:
template <typename... Ts>
struct type_list
{
static constexpr size_t length = sizeof...(Ts);
template <typename T>
using push_front = type_list<T, Ts...>;
template <typename T>
using push_back = type_list<Ts..., T>;
// hidden implementation of complex "methods"
template <uint64_t index>
using at;
struct pop_front;
template <typename U>
using concat;
template <uint64_t index>
struct split;
template <uint64_t index, typename T>
using insert;
template <uint64_t index>
using remove;
};在另一段代码中,我有这样一个类型的打字员TL,它静态地继承一个基类,提供这样一个接口:
template<typename Derived>
struct Expression {
using type1 = typename Derived::_type1;
using type2 = typename Derived::_type2;
};
struct Exp1 : Expression<Exp1> {
template<typename> friend struct Expression;
private:
using _type1 = float;
using _type2 = int;
};
struct Exp2 : Expression<Exp2> {
template<typename> friend struct Expression;
private:
using _type1 = double;
using _type2 = short;
};我想从TL生成嵌套类型的打字员,如下所示:
using TL = type_list<Exp1, Exp2>;
using TL2 = type_list<TL::type1...>; // type_list<float, double>但是我不能展开TL,因为它不是一个未展开的参数包。
我考虑过index_sequence,但无法使它工作。
发布于 2022-07-20 15:50:41
这个问题似乎是在寻找map,也就是transform in C++。TL是一个类型列表,它希望应用某种类型级别的函数(提取::type 1),并有另一个类型列表。书写转换很简单:
template <template <typename> typename fn, typename TL>
struct type_list_transform_impl;
template <template <typename> typename fn, typename... Ts>
struct type_list_transform_impl<fn, type_list<Ts...>>
{
using type = type_list<fn<Ts>...>;
};
template <template <typename> typename fn, typename TL>
using type_list_transform = type_list_transform_impl<fn, TL>::type;然后是类型级函数:
template <typename T>
using type1_of = typename T::type1;并将这些片段组合起来,以获得TL2:
using TL2 = type_list_transform<type1_of, TL>; // type_list<float, double>https://stackoverflow.com/questions/73051519
复制相似问题