Home > Back-end >  c Variadic boost fusion map alias template
c Variadic boost fusion map alias template

Time:10-18

Consider this snippet:

#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

I'm struggling with the alias template MsgSignals.

What is the right syntax such that the type of signals becomes

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

CodePudding user response:

You can use template partial specialization to extract the types in 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.

  • Related