如何使用brigand转换此类型列表?

时间:2018-11-18 23:05:45

标签: c++ c++11 templates variadic-templates template-meta-programming

我有以下类型的列表:

using ComponentList = brigand::list<TransformComponent, ObjectComponent, BodyComponent>

如何将以前的列表转换为如下所示的新列表:

using ComponentHandleList = brigand::list<entityx::ComponentHandle<TransformComponent>, entityx::ComponentHandle<ObjectComponent>, entityx::ComponentHandle<BodyComponent>>;

我基本上想生成一个新列表,其中包含前一个列表中每个元素的包装。 我尝试通过使用以下方法来做到这一点:

using ComponentHandleList = brigand::transform<ComponentList, AddComponentHandle<brigand::_1>>;

但是我对元编程了解不多,并且未能实现struct AddComponentHandle<>,这将采用类型T并将其转换为entityx::ComponentHandle<T>。 Brigand包含有关转换类型列表的相关documentation,其中显示了一个使用T将每个类型T*转换为std::add_pointer<>的示例。我想做的只是从Tentityx::ComponentHandle<T>。如何转换第一个列表,使其看起来像第二个列表?

2 个答案:

答案 0 :(得分:0)

您可以使用

进行转换
template<typename, template<typename...> class>
struct apply {};

template<template<typename...> class T, template<typename...> class List, typename ... Ts>
struct apply<List<Ts...>, T> {
    using type = List<T<Ts>...>;
};

可以这样使用

template<typename...>
struct A {};

template<typename...>
struct B {};

int main()
{
    static_assert(std::is_same<apply<A<int, bool>, B>::type, A<B<int>, B<bool>>>::value);
}

答案 1 :(得分:0)

#include <brigand/sequences/list.hpp>
#include <brigand/algorithms/transform.hpp>
#include <type_traits>

struct TransformComponent{};
struct ObjectComponent{};
struct BodyComponent{};
namespace entityx
{
    template <typename Component>
    struct ComponentHandle{};
}

using ComponentList = brigand::list<TransformComponent, ObjectComponent, BodyComponent>;

template <typename Comp>
using AddComponentHandle = entityx::ComponentHandle<Comp>;

template <typename Comp>
struct AddComponentHandle2
{
    using type = entityx::ComponentHandle<Comp>;
};

using ComponentHandleList = brigand::transform<ComponentList, brigand::bind<AddComponentHandle,brigand::_1>>;
using ComponentHandleList2 = brigand::transform<ComponentList, AddComponentHandle2<brigand::_1>>;


int main()
{
    static_assert(std::is_same<ComponentHandleList,
                                brigand::list<
                                    entityx::ComponentHandle<TransformComponent>,
                                    entityx::ComponentHandle<ObjectComponent>,
                                    entityx::ComponentHandle<BodyComponent>
                                >
                  >::value
                 );
    static_assert(std::is_same<ComponentHandleList2,
                                brigand::list<
                                    entityx::ComponentHandle<TransformComponent>,
                                    entityx::ComponentHandle<ObjectComponent>,
                                    entityx::ComponentHandle<BodyComponent>
                                >
                  >::value
                 );
}