简体   繁体   中英

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?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM