In one of my projects I needed to map the int
of the boost::variant
which()
-Function to the types of the boost::variant.
For some reasons the map doesn't contain the correct TVar Type? Why is that?
#include <boost/mpl/for_each.hpp>
#include <boost/variant.hpp>
#include <string>
#include <map>
#include <iostream>
using TVar = boost::variant<std::string, int, double>;
namespace Helper {
struct createMap {
std::map<int, TVar> map;
template<typename T>
void operator()(const T& t) {
auto x = TVar(t);
map[x.which()] = x;
}
};
}
bool createObject(int which, TVar& var) {
Helper::createMap c;
boost::mpl::for_each<TVar::types>(boost::ref(c));
if (c.map.find(which) != c.map.end()) {
var = c.map[which];
return true;
}
else {
return false;
}
}
int main() {
TVar var;
bool ok=createObject(0, var);
return 0;
}