c ++ Variadic boost融合图别名模板

c++ Variadic boost fusion map alias template

考虑这个片段:

#include <boost/fusion/container/map.hpp>
#include <boost/fusion/include/pair.hpp>

struct MsgA {};
struct MsgB {};

using MsgList = std::tuple<MsgA, MsgB>;

template <typename Msg>
class MsgSignal {};

template <typename... Args>
using MsgSignals =
    boost::fusion::map<boost::fusion::pair<Args, MsgSignal<Args>>, ...>;

int main() {
    MsgSignals<MsgList> signals;
    // signals should be of type boost::fusion::map<boost::fusion::pair<MsgA, MsgSignal<MsgA>,
                                                    boost::fusion::pair<MsgB, MsgSignal<MsgB>>>                                     >
}

Demo

我正在努力使用别名模板 MsgSignals

什么是正确的语法使得 signals 的类型变成

boost::fusion::map<boost::fusion::pair<MsgA, MsgSignal<MsgA>,
                   boost::fusion::pair<MsgB, MsgSignal<MsgB>>> 

您可以使用模板偏特化来提取 std::tuple 中的类型:

template <typename Tuple>
struct MsgSignalsImpl;

template <typename... Args>
struct MsgSignalsImpl<std::tuple<Args...>> {
  using type = boost::fusion::map<boost::fusion::pair<Args, MsgSignal<Args>>...>;
};

template <typename Tuple>
using MsgSignals = typename MsgSignalsImpl<Tuple>::type;

Demo.