0

So I've built my boost::mpl::map object which contains some mixture of keys and types. I now want to instantiate an instance of the map such that I have an instantiation of each type in the map:

using namespace boost::mpl;
using MyMap = map<
  pair<int, double>,
  pair<double, double>,
  pair<bool, int>>;

int main()
{
  // create the map:
  MyMap myMap;
  // now I want a reference to the element indexed by "int"
  using RefType = at<MyMap, int>::type&;

  RefType myRef(myMap); // compile error!
}

The error I'm getting is something along the lines of:

error: cannot convert 'MyMap {aka map<...>} to blarghh {aka int}

Clearly I should be getting some sort of "index" value (maybe boost::mpl::map::order?). So how does one actually access (ie get references to) elements in these associative mpl containers? Also, where is the documentation for how this (sepcifically) is done?

quant
  • 21,507
  • 32
  • 115
  • 211

1 Answers1

0

Your code had a few problems:

  • No #includes. Missing <boost/mpl/at.hpp> is a problem here.
  • No commas between the pairs in the map.
  • You're trying to create a map as a value, which doesn't make sense. I'm not sure what to make of MyMap myMap. You don't usually instantiate MPL types.

This code works, hopefully it's roughly what you want.

#include <boost/mpl/map.hpp>
#include <boost/mpl/at.hpp>

using namespace boost::mpl;
using MyMap = map<
  pair<int, double>,
  pair<double, double>,
  pair<bool, int>>;

int main()
{
  using RefType = at<MyMap, int>::type&;

  double foo;
  RefType myRef(foo);
}
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Sorry, I fixed the missing commas. I want to instantiate the map such that I have a set of all `types` instantiated, then I want a reference to one of the values (for the sake of this example). The `std` equivalent might be creating an instantiation of a `tuple` and getting a reference to one of its instantiated types. Does this make sense? Your example, as I understand it, just creates a double and a reference to it. – quant Oct 01 '14 at 03:12
  • I'm thinking maybe I need boost fusion for this? In that case, my next question is; how do I convert my `boost::mpl::map` to a `boost::fusion::map` that I can then instantiate? – quant Oct 01 '14 at 03:19
  • What is the higher-level goal you're trying to accomplish? I'm having a hard time understanding why you would want a map of `{type: default-constructed-other-type}`. – John Zwinck Oct 01 '14 at 05:47