将所有 boost fusion map keys 收集到 std::tuple

Collect all boost fusion map keys into a std::tuple

考虑这个片段:

#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::EventAMyEvents::EventB,因此 AllKeys = std::tuple<MyEvents::EventA, MyEvents::EventB>

如何使用模板专业化来做到这一点?我需要某种递归调用吗?

您可以使用T::first_type获取boost::fusion::pair的密钥类型。

template <typename T>
struct GetTypes;

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

Demo.