代码之家  ›  专栏  ›  技术社区  ›  Juergen

将所有boost fusion映射键收集到std::tuple中

  •  0
  • Juergen  · 技术社区  · 3 年前

    #include <boost/fusion/container/map.hpp>
    #include <boost/fusion/sequence/intrinsic/at_key.hpp>
    #include <boost/fusion/sequence/intrinsic/value_at_key.hpp>
    #include <tuple>
    
    struct MyEvents {
        struct EventA;
        struct EventB;
        using EventMap = boost::fusion::map<boost::fusion::pair<EventA, int>,
                                            boost::fusion::pair<EventB, double>>;
    };
    
    template <typename T>
    struct GetTypes;
    
    template <typename T>
    struct GetTypes<boost::fusion::map<...>> {
        using type = std::tuple<...>;
    };
    
    int main() {
        using Map = typename MyEvents::EventMap;
        using AllKeys = GetTypes<Map>::type;
    
        return 0;
    }
    

    Demo

    我想从一个数据库中收集所有密钥类型 boost::fusion::map std::tuple .

    MyEvents::EventA MyEvents::EventB AllKeys = std::tuple<MyEvents::EventA, MyEvents::EventB> .

    如何使用模板专门化实现这一点?我需要某种递归调用吗?

    1 回复  |  直到 3 年前
        1
  •  2
  •   康桓瑋    3 年前

    boost::fusion::pair 利用 T::first_type .

    template <typename T>
    struct GetTypes;
    
    template <typename... Pairs>
    struct GetTypes<boost::fusion::map<Pairs...>> {
        using type = std::tuple<typename Pairs::first_type...>;
    };
    

    Demo.

    推荐文章