Reputation: 1975
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 ?
Upvotes: 1
Views: 562
Reputation: 3093
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, "");
Upvotes: 2