c++ - template method specialization for ptr to struct and vector of ptr to struct? -


if have variant contain 2 types- pointer struct, , vector of pointer struct. how specialize 2 template methods in boost::static_visitor handle 2 cases?

    boost::variant<a*,b*,c*,vector<a*>*, vector<b*>*, vector<c*>*> 

have visitor functor inherit static_visitor<void>, or typedef void result_type. have override of each type in variant -- can via templates, or explicit overrides.

struct my_visitor: static_visitor<void> {   void operator()( a* ) const {     std::cout << "i see a*\n";   }   void operator()( b* ) const {     std::cout << "i see b*\n";   }   void operator()( c* ) const {     std::cout << "i see c*\n";   }   void operator()( std::vector<c*>* ) const {     std::cout << "i see std::vector<c*>*\n";   }   void operator()( std::vector<b*>* ) const {     std::cout << "i see std::vector<b*>*\n";   }   void operator()( std::vector<a*>* ) const {     std::cout << "i see std::vector<a*>*\n";   } }; 

as aside, storing raw pointers in boost::variant seems poor idea -- i'd want store smart pointers or instances of object. similar storing pointers std::vector. there use cases store raw pointers, not common.

a boost::variant< a, b, c, std::vector<a>, std::vector<b>, std::vector<c> >, or if types polymorphic interfaces, std::shared_ptr same (last checked boost::variant not move-aware, std::unique_ptr doesn't work it).

imagine if want ignore std::vector instances; struct my_visitor: static_visitor { void operator()( a* ) const { std::cout << "i see a*\n"; } void operator()( b* ) const { std::cout << "i see b*\n"; } void operator()( c* ) const { std::cout << "i see c*\n"; } template void operator()( std::vector* ) const { std::cout << "i see std::vector*\n"; } }; or, if wanted ignore looks container:

// container traits class, detects if `std::begin` , `std::end` work on instance of data: template<typename c, bool=true> struct is_container:std::false_type {}; template<typename c> struct is_container<c, std::is_same< std::begin(std::declval<c&>()), std::end(std::declval<c&>()) >::value>:   std::true_type {};   struct my_visitor: boost::static_visitor<void> {   // blah blah blah    // sfinae detection of c being container:   template<typename c>   typename std::enable_if< is_container<c>::value >::type operator()( c* ) const {     std::cout << "i see container<?>*\n";   } }; 

in c++11 compiler.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -