[Boost-bugs] [Boost C++ Libraries] #9479: polymorphic_get<> for variant

Subject: [Boost-bugs] [Boost C++ Libraries] #9479: polymorphic_get<> for variant
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2013-12-07 12:31:35


#9479: polymorphic_get<> for variant
------------------------------+-----------------------
 Reporter: apolukhin | Owner: apolukhin
     Type: Feature Requests | Status: new
Milestone: To Be Determined | Component: variant
  Version: Boost 1.54.0 | Severity: Problem
 Keywords: |
------------------------------+-----------------------
 The original idea was described [http://coliru.stacked-
 crooked.com/a/3e920faa604fa534 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;
 }
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/9479>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:15 UTC