Opened 9 years ago

Closed 9 years ago

#9479 closed Feature Requests (fixed)

polymorphic_get<> for variant

Reported by: Antony Polukhin Owned by: Antony Polukhin
Milestone: Boost 1.56.0 Component: variant
Version: Boost 1.54.0 Severity: Problem
Keywords: Cc:

Description

The original idea was described here:

#include <iostream>
#include <boost/variant.hpp>
#include <boost/variant/get.hpp>

//------------------------------------------------------------------------------
struct unused
{
    template<class T>
    unused(T&) {}
};

template<class T>
struct Getter
{
    typedef T* result_type;
    
    T* operator()(T& v) const
    {
        return &v;
    }

    T* operator()(unused) const
    {
        return nullptr;
    }
};

template<class U, class T>
U* poly_get(T* v)
{
    return boost::apply_visitor(Getter<U>(), *v);
}
//------------------------------------------------------------------------------

struct O{
};
struct A:O{
};
struct B:O{
};


int main(int argc, char** argv) {
    boost::variant<A, B> v;
    std::cout << "boost::get-------------------------------\n";
    std::cout << boost::get<O>(&v) << std::endl;
    std::cout << boost::get<A>(&v) << std::endl;
    std::cout << boost::get<B>(&v) << std::endl;
    std::cout << "poly_get---------------------------------\n";
    std::cout << poly_get<O>(&v) << std::endl;
    std::cout << poly_get<A>(&v) << std::endl;
    std::cout << poly_get<B>(&v) << std::endl;
}

Change History (3)

comment:1 by Antony Polukhin, 9 years ago

In git-commit added docs, tests and implementation of polymorphic_get method.

comment:2 by Antony Polukhin, 9 years ago

Status: newassigned

comment:3 by Antony Polukhin, 9 years ago

Milestone: To Be DeterminedBoost 1.56.0
Resolution: fixed
Status: assignedclosed
Note: See TracTickets for help on using tickets.