0

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;
}
Aleph0
  • 5,816
  • 4
  • 29
  • 80

1 Answers1

0

If I understand correctly, you want to assign the variant with a default constructed value of some type that will be determined at run-time by indexing possible types for the variant, then you are looking for this:

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

#include <boost/variant.hpp>
#include <string>


template <typename VariantT, int L, int R>
struct assign_default_constructed
{
    static bool call(int which, VariantT& var)
    {
        static int const M = L + (R - L + 1) / 2;
        if (which < M) {
            return assign_default_constructed<VariantT, L, M - 1>::call(which, var);
        }
        else {
            return assign_default_constructed<VariantT, M, R>::call(which, var);
        }
    }
};

template <typename VariantT, int I>
struct assign_default_constructed<VariantT, I, I>
{
    static bool call(int /*which*/, VariantT& var)
    {
        //assert(which == I);
        var = typename boost::mpl::at_c<typename VariantT::types, I>::type();
        return true;
    }
};

template <typename VariantT>
bool createObject(int which, VariantT& var)
{
    static int const N = boost::mpl::size<typename VariantT::types>::value;
    if (which < 0 || which >= N) return false;
    return assign_default_constructed<VariantT, 0, N - 1>::call(which, var);
}

int main() {
    boost::variant<std::string, int, double> var;
    bool ok = createObject(1, var);
    return ok ? var.which() : -1;
}
Nikita Kniazev
  • 3,728
  • 2
  • 16
  • 30