1

I'm using different boost::variant types throughout my projects. For example I have

typedef boost::variant<A1, A2> TA;
typedef boost::variant<B1, B2, B3> TB;
typedef boost::variant<A1, B2> TC;

In order to transport these different boost::variant types across a unified interface I'm using boost::any. For example:

TA a=A1();
boost::any container=a;

Now extracting the data A1 in this case from container becomes a bit tedious. I have to write explicitly

A1 a=boost::get<A1>(boost::any_cast<TA>(container));

I just wondered, if it might be possible to implement a template helper function like this:

template<typename T>
T getValue<T>(const boost::any& any) {
 auto var=boost::any_cast</* What to put here*/>(container);
 return boost::get<T>(var);
}

But here I'm really baffled what type to write into the boost::any_cast.

Partial solution:

After refactoring the code of an answer of a previous question of mine, I was able to derive the following partial solution of my problem.

Unfortunately, the solution has still two serious drawbacks.

Firstly, I don't know how to rule out all boost::variants in Variants that are not able to contain the requested type T.

Secondly, I have to explicitly mention the list of my possible boost::variant types. I consider this restriction not so severe like the first one.

#include <boost/variant.hpp>
#include <boost/any.hpp>
#include <boost/optional.hpp>
#include <boost/mpl/for_each.hpp>
#include <boost/mpl/find_if.hpp>
#include <boost/mpl/remove_if.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/joint_view.hpp>
#include <vector>
#include <iostream>

template<class T, class V>
struct variantToType
{
    variantToType(const V& value) : value(value) {  }

    boost::optional<T> result;
    V value;
    void operator()(const T& t)
    {
        if (T* val = boost::get<T>(&value)) {
            result = boost::make_optional<T>(*val);
        } else {
            result = boost::none;
        }
    }
};

template<typename T>
struct anyToVariant {
    anyToVariant(const boost::any& value) : value(value) { }

    boost::optional<T> result;
    const boost::any value;
    template<typename VARIANT>
    void operator()(const VARIANT&) {
        try {
            VARIANT var=boost::any_cast<VARIANT>(value);    

            variantToType<T, VARIANT> generator(var);
            boost::mpl::for_each<VARIANT::types>(boost::ref(generator));
            if (generator.result) {
                result = generator.result;
            }
        }
        catch (const boost::bad_any_cast&) {}
    }
};

template<class T>
boost::optional<T> convert(const boost::any&value) {
    using namespace boost; 
    anyToVariant<T> x(value);
    //using AllTypes = boost::mpl::joint_view<V1::types, V2::types>; // Of no help?

    using Variants = mpl::vector<V1, V2>::type;
    mpl::for_each<Variants>(boost::ref(x));
    return x.result;
}

typedef boost::variant<int> V1;
typedef boost::variant<double, int> V2;

int main(int argc, char**args) {
    try {
        {
            V1 x = 5;
            boost::any any = x;
            boost::optional<int> y = convert<int>(any);
            std::cout << y.value() << std::endl;
        }
        {
            V2 x = 5.5;
            boost::any any = x;

//          This will not compile due to boost::get, as V1 does not contain a double type. :-(
//          boost::optional<double> y = convert<double>(any);
//          std::cout << y.value() << std::endl;
        }
    }
    catch (const std::exception& e) {
        std::cout << e.what() << std::endl;
    }
    return 0;
}
Aleph0
  • 5,816
  • 4
  • 29
  • 80
  • I really doubt this can be done. What you may want to do is have `getValue` templated on both `T` and `TA`. So in order to extract the desired value you would do `getValue(container)`. – linuxfever Jul 14 '16 at 09:24
  • Possible duplicate of [Generic function to convert boost::any to boost::variant](http://stackoverflow.com/questions/35357614/generic-function-to-convert-boostany-to-boostvariant) – m.s. Jul 14 '16 at 11:51

0 Answers0