Consider,
I get compilation error at boost::apply_visitor. The question: How do I pass a dependent type to boost::apply_visitor?Code:# include <iostream> # include <map> # include <boost/variant.hpp> # include <boost/mpl/vector.hpp> # include <boost/mpl/contains.hpp> # include <boost/bind.hpp> # include <boost/utility/enable_if.hpp> // Generic visitor that does magical dispatching of // types and delegates passes down to your visitor only // those types specified in a type list. template <typename Visitor, typename TypeList> struct lets_see : public boost::static_visitor<void>, public Visitor { template <typename T> inline void operator () ( T v, typename boost::enable_if< typename boost::mpl::contains< TypeList, T >::type >::type *dummy = NULL ) const { Visitor::operator () (v); } template <typename T> inline void operator () ( T v, typename boost::disable_if < typename boost::mpl::contains< TypeList, T >::type >::type *dummy = NULL ) const {} }; struct nil { int x ; }; struct example_visitor { typedef lets_see < example_visitor, boost::mpl::vector<nil, char, int > > value_type; inline void operator () (char v) const { std::cout << "character detected" << std::endl; } inline void operator () (int v) const { std::cout << "integer detected=" << v << std::endl; } inline void operator () (nil v) const { std::cout << "nil detected=" << v.x << std::endl; } }; template < typename T > class Foo { protected : typedef boost::variant < nil, char, int, double > sql_field; T visitor ; typedef std::map< unsigned int , Foo <T>* > FooMap; static FooMap mFooMap; public : static Foo <T>* createInstance( unsigned int const ); void Register( unsigned int const, Foo< T >* ); virtual void doWork () ; }; template < typename T> typename Foo<T>::FooMap Foo<T>::mFooMap; template < typename T> Foo<T>* Foo<T>::createInstance( unsigned int const in ) { FooMap::iterator it = mFooMap.find( in ); if ( it != mFooMap.end() ) { return ( it->second ); } else { return 0 ; } } template < typename T> void Foo<T>::Register( unsigned int const in, Foo<T>* factory ) { mFooMap.insert( FooMap::value_type( in, factory ) ); } template < typename T> void Foo<T>::doWork() {} template <typename T> class Bar : public Foo <T> { // Bar( const bar& ); public : Bar( unsigned int a ) { Register ( a, this ) ; } static Bar mBar; void doWork () { sql_field intField ( 1 ); // using Foo<T>::visitor ; boost::apply_visitor ( typename Foo<T>::visitor(), intField ); // boost::apply_visitor ( Foo<T>::visitor(), intField ); std::cout << "Work done" << std::endl; } }; template < typename T> Bar<T> Bar<T>::mBar; int main() { try { typedef Foo <example_visitor::value_type> FooValueType; //Bar <example_visitor::value_type> b ( 5 ); Bar <FooValueType> b ( 5 ); Foo <FooValueType>* ptr = Foo<FooValueType>::createInstance ( 5 ) ; if ( ptr ) { ptr->doWork(); } } catch ( const std::exception& e ) { std::cout << e.what() << std::endl; } std::cout << "[done]" << std::endl; std::cin.get() ; }


Reply With Quote
Bookmarks