Skip to content

Latest commit

 

History

History
50 lines (39 loc) · 1.47 KB

README.md

File metadata and controls

50 lines (39 loc) · 1.47 KB

poly2variant

poly2variant is an experience using a variant like Boost.Variant as a safe type switching mechanism to discover derived types from bases when there are a known set of polymorphic types.

This solution is safer and faster than a bundle of dynamic_casts but is not better than Poly Visitor. The last complains at compile time about new types introduced in the class hierarchy and it is more efficient too.

Maybe, the only reason to use poly2variant versus Poly Visitor is its non-intrusive approach in the class hierarchy: the last demands the addition of visitable macros.

Building blocks

poly2variant takes a variant type and a polymorphic object and returns a variant.

apply_visitor visits a polymorphic object with a visitor (It constructs a variant under the hood.)

Demo

using visitables = boost::mpl::vector<cockatiel, eagle>;

struct bird { virtual ~bird() = default; };
struct cockatiel : bird {};
struct eagle : bird {};
    
struct visitor
{
    using result_type = void;
    using visitables = ::visitables;
    void operator()(const cockatiel* o) const
    { std::cout << "cockatiel" << std::endl; }
    void operator()(const eagle* o) const
    { std::cout << "eagle" << std::endl; }
};

int main()
{
    cockatiel o;
    const bird* base = &o;
    poly2variant::apply_visitor(visitor{}, base);
}