c++boostc++17metaprogrammingboost-mpl

Use the value of strongly typed enum as index in boost::mpl::map


I am using a C++ map structure defined similar to std::map<Foo, std::any> for storing attributes of a compiler symbol table. The Foo is a strongly typed enum, such as enum class Foo {ENUM}. After casting some other types to std::any, I need std::any_cast to cast it back if I access the entry by the Foo::ENUM key. The type parameter of std::any_cast is tied to Foo::ENUM.

That being said, I want something for automatically determining the type parameter for std::any_cast based on Foo::ENUM as following:

std::map<Foo, std::any> m;
const auto x = std::any_cast<some_magic(Foo::ENUM)::type>(m[Foo::ENUM]);

Here is my current implementation:

#include <iostream>
#include <string>
#include <vector>
#include <type_traits>
#include <any>
#include <map>

#define CAT(x, y) CAT_(x, y)
#define CAT_(x, y) x ## y

#define ENABLE_ENUM_TO_TYPE(enum_class_name) \
template <enum_class_name> struct CAT(enum_class_name, _enum_to_type);

#define ENUM_TYPE_PAIR(enum_class_name, enum_key, type_) \
template <> struct CAT(enum_class_name, _enum_to_type)<enum_class_name::enum_key> {using type = type_;};

enum class Foo {
  A1, A2
};

enum class Bar {
  B1, B2, B3
};

ENABLE_ENUM_TO_TYPE(Foo);
ENABLE_ENUM_TO_TYPE(Bar);
ENUM_TYPE_PAIR(Foo, A1, int);
ENUM_TYPE_PAIR(Foo, A2, double);
ENUM_TYPE_PAIR(Bar, B1, std::string);
ENUM_TYPE_PAIR(Bar, B2, std::vector<int>);

template <auto T1>
constexpr auto enum_to_type_impl() {
#define COMPARE_ENUM_CLASS(enum_class_name) \
if constexpr (std::is_same_v<decltype(T1), enum_class_name>) { return CAT(enum_class_name, _enum_to_type)<T1>{};} else
  COMPARE_ENUM_CLASS(Foo)
  COMPARE_ENUM_CLASS(Bar)
#undef COMPARE_ENUM_CLASS
{}
}

template <auto EnumVal>
struct enum_to_type {
  using type = typename decltype(enum_to_type_impl<EnumVal>())::type;
};

int main() {
  std::map<Bar, std::any> m;
  m[Bar::B1] = std::string{"Hello"};
  std::cout << std::any_cast<enum_to_type<Bar::B1>::type>(m[Bar::B1]) << std::endl;
  return 0;
}

However, my code above is still cumbersome. I look into the boost mpl library but apparently it does not offer something like mpl::enum_, and I have tried the following code but it does not work:

#include <boost/mpl/map.hpp>
#include <boost/mpl/pair.hpp>
#include <boost/mpl/at.hpp>
#include <string>
#include <iostream>

enum class Bar {
  C1, C2
};

template <auto EnumVal> struct enum_
{
  using type = enum_;
  using value_type = decltype(EnumVal);
  typedef mpl_::integral_c_tag tag;
};


typedef boost::mpl::map<
  boost::mpl::pair<enum_<Bar::C1>, std::string>,
  boost::mpl::pair<enum_<Bar::C2>, int>
  > m;

int main() {
  boost::mpl::at<m, enum_<Bar::C1>>::type x;
  return 0;
}

When compiling the code above, I get the following error message:

main.cpp: In function ‘int main()’:
main.cpp:24:38: error: incomplete type ‘boost::mpl::at<boost::mpl::map<boost::mpl::pair<enum_<Bar::C1>, std::__cxx11::basic_string<char> >, boost::mpl::pair<enum_<Bar::C2>, int> >, enum_<Bar::C1> >’ used in nested name specifier
   24 |   boost::mpl::at<m, enum_<Bar::C1>>::type x;

Any ideas? Is the data type for boost::mpl::map extensible?

EDIT: I have added #include <boost/mpl/at.hpp> in the above code snipet following the accepted answer, and it works properly. As for the boost::mp11 solution, my code is on https://godbolt.org/z/KvjecnTe3, but gcc fails to compile it, and I have submitted an issue on https://github.com/boostorg/mp11/issues/72.


Solution

  • You need to include #include <boost/mpl/at.hpp>. Also, I wouldn't recommend using boost::mpl - it's really slow to compile. Your "cumbersome" solution is fastest IMHO, and if you don't like it you might like to look towards boost::mp11 or any other "more modern" metaprogramming library.