Gabriel
Gabriel

Reputation: 9432

how to get the i-th underlying type (by an const integer) of af boost::variant

I would like to get the underlying type of a variant. For example"something similar to this:

typedef boost::variant< shared_ptr<int> , shared_ptr<float> > VType;

typedef VType::get<0>::type  TYPE1;       // TYPE1 = shared_ptr<int> 

TYPE1 value = new std::remove_pointer<TYPE1>::type()    // make a new  shared_ptr<int>

Any ideas how to achieve this with boost?

Upvotes: 1

Views: 73

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254471

The variant contains an MPL sequence called types, which you can use to access the types.

#include <boost/mpl/at.hpp>

typedef typename boost::mpl::at_c<VType::types, 0>::type TYPE1;

Upvotes: 3

Related Questions