1

Is something like following

typedef boost::mpl::map<
      pair<int,"int">
    , pair<long,"long">
    , pair<bool,"bool">
    > m;

possible? If not, what are the alternatives ?

Recker
  • 1,915
  • 25
  • 55
  • 3
    I think you want a `boost::fusion::map`. I guess that using `boost::mpl:: string` as the second parameter in the `boost::mpl::pair` could also work. – llonesmiz Sep 27 '15 at 08:36

1 Answers1

2

If you can use a C++14 compiler (currently only Clang >= 3.5), you can use Boost.Hana:

#include <boost/hana.hpp>
namespace hana = boost::hana;

auto m = hana::make_map(
    hana::make_pair(hana::type_c<int>, BOOST_HANA_STRING("int")),
    hana::make_pair(hana::type_c<long>, BOOST_HANA_STRING("long")),
    hana::make_pair(hana::type_c<bool>, BOOST_HANA_STRING("bool"))
);

// These assertions are done at compile-time
BOOST_HANA_CONSTANT_ASSERT(m[hana::type_c<int>] == BOOST_HANA_STRING("int"));
BOOST_HANA_CONSTANT_ASSERT(m[hana::type_c<long>] == BOOST_HANA_STRING("long"));
BOOST_HANA_CONSTANT_ASSERT(m[hana::type_c<bool>] == BOOST_HANA_STRING("bool"));

If you're also willing to use a non-standard GNU extension supported (at least) by Clang and GCC, you can even do the following and drop the ugly BOOST_HANA_STRING macro:

#define BOOST_HANA_CONFIG_ENABLE_STRING_UDL

#include <boost/hana.hpp>
namespace hana = boost::hana;
using namespace hana::literals;

constexpr auto m = hana::make_map(
    hana::make_pair(hana::type_c<int>, "int"_s),
    hana::make_pair(hana::type_c<long>, "long"_s),
    hana::make_pair(hana::type_c<bool>, "bool"_s)
);

static_assert(m[hana::type_c<int>] == "int"_s, "");
static_assert(m[hana::type_c<long>] == "long"_s, "");
static_assert(m[hana::type_c<bool>] == "bool"_s, "");
Louis Dionne
  • 3,104
  • 1
  • 15
  • 35