Boost Variant: how to get currently held type?

后端 未结 4 2142
感情败类
感情败类 2020-12-29 01:27

As I understood all types of boost.variant are parsed into real types (meaning as if boost variant a; a=\"bla-bla\" would after compilation t

4条回答
  •  余生分开走
    2020-12-29 02:17

    boost.variant has a .type() function which can return the typeid of the active type, provided you've enabled RTTI.

    You could also define a static visitor to perform actions depending on the type of content of the variant, e.g.

    struct SomeVisitor : public boost::static_visitor
    {
        double operator()(const func0& f0) const { return f0(1.0); }
        double operator()(const func1& f1) const { return f1(1.0, 1.0); }
        double operator()(int integer) const { return integer; }
    };
    ...
    std::cout << boost::apply_visitor(SomeVisitor(), v) << std::endl;
    

提交回复
热议问题