开发者

Iterating over the types in a boost::variant

开发者 https://www.devze.com 2022-12-19 03:17 出处:网络
I\'m using a boost variant to hold some generated types, right now my开发者_Python百科 code generator creates a header with the types and a variant capable of holding them.At initialization time, I\'d

I'm using a boost variant to hold some generated types, right now my开发者_Python百科 code generator creates a header with the types and a variant capable of holding them. At initialization time, I'd like to iterate over the allowable types in the variant, not the types the variant is holding at the moment.

Can I do this with a variant?


boost::variant exposes its types via types, which is an MPL list. You can do runtime operations over MPL lists using mpl::for_each:

struct printer {
    template<class T> void operator()(T t) {
        std::cout << typeid(T).name() << std::endl;
    }
};

// ... 
typedef boost::variant<int, char> var;
boost::mpl::for_each<var::types>(printer());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号