c++ - boost::variant & STL containers - Am I doing it wrong? -


i'm scratching head on one. i'm trying use boost::variant store vector of values of type determined @ run time. vector's internal type doesn't change once known. trying create interface code can add value vector , later retrieve values vector. code not compile. approach wrong? should using polymorphism job? code follows (does not compile):

template <typename t> class record_value_visitor : public boost::static_visitor<void> { public:     record_value_visitor(t v) : _val(v) {}      void operator()(vector<t>& vec) const     {         vec.push_back(this->_val);     }  private:     t _val; };   class monitor { public:     monitor(int type)     {          switch (type) {             case 1:                 this->_values = vector<int>();                 break;             case 2:                 this->_values = vector<string>();                 break;             default:                 throw new exception("invalid type");         }          this->_type = type;     }       void add_value(int val)      {          if ( this->_type != 1 ) throw new runtime_error("invalid type add_value()");          boost::apply_visitor( record_value_visitor<int>(val), this->_values);     }     void add_value(string val)     {          if ( this->_type != 2 ) throw new runtime_error("invalid type add_value()");          boost::apply_visitor( record_value_visitor<string>(val), this->_values);     }  private:     int _type;     boost::variant< vector<int>, vector<double>, vector<string> > _values; };   int main(int argc, char* argv[]) {     monitor int_mon(1);     monitor str_mon(2);      int_mon.add_value(4);     str_mon.add_value(string("foo"));       return 0; } 

edit: add error message

    1>c:\boost_1_46_1\boost\variant\variant.hpp(832): error c2664: 'void record_value_visitor<t>::operator ()(const std::vector<_ty> &) const' : cannot convert parameter 1 't1' 'const std::vector<_ty> &' 1>          1>          [ 1>              t=int, 1>              _ty=int 1>          ] 1>          , 1>          [ 1>              _ty=int 1>          ] 1>          reason: cannot convert 't1' 'const std::vector<_ty>' 1>          1>          [ 1>              _ty=int 1>          ] 1>          no user-defined-conversion operator available can perform conversion, or operator cannot called 1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(145) : see reference function template instantiation 'void boost::detail::variant::invoke_visitor<visitor>::internal_visit<t>(t &,int)' being compiled 1>          1>          [ 1>              visitor=const record_value_visitor<int>, 1>              t=t1 1>          ] 1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(173) : see reference function template instantiation 'void boost::detail::variant::visitation_impl_invoke_impl<visitor,voidptrcv,t>(int,visitor &,voidptrcv,t *,boost::mpl::false_)' being compiled 1>          1>          [ 1>              visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>, 1>              voidptrcv=void *, 1>              t=t1 1>          ] 1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(256) : see reference function template instantiation 'void boost::detail::variant::visitation_impl_invoke<visitor,voidptrcv,t1,nobackupflag>(int,visitor &,voidptrcv,t *,nobackupflag,int)' being compiled 1>          1>          [ 1>              visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>, 1>              voidptrcv=void *, 1>              nobackupflag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_, 1>              t=t1 1>          ] 1>          c:\boost_1_46_1\boost\variant\variant.hpp(1776) : see reference function template instantiation 'void boost::detail::variant::visitation_impl<first_which,first_step,visitor,voidptrcv,boost::variant<t0_,t1,t2>::has_fallback_type_>(const int,const int,visitor &,voidptrcv,boost::mpl::false_,nobackupflag,which *,step0 *)' being compiled 1>          1>          [ 1>              visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>, 1>              voidptrcv=void *, 1>              t0_=std::vector<int>, 1>              t1=std::vector<double>, 1>              t2=std::vector<std::string>, 1>              nobackupflag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_, 1>              which=first_which, 1>              step0=first_step 1>          ] 1>          c:\boost_1_46_1\boost\variant\variant.hpp(1787) : see reference function template instantiation 'void boost::variant<t0_,t1,t2>::internal_apply_visitor_impl<visitor,void*>(int,int,visitor &,voidptrcv)' being compiled 1>          1>          [ 1>              t0_=std::vector<int>, 1>              t1=std::vector<double>, 1>              t2=std::vector<std::string>, 1>              visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>, 1>              voidptrcv=void * 1>          ] 1>          c:\boost_1_46_1\boost\variant\variant.hpp(1810) : see reference function template instantiation 'void boost::variant<t0_,t1,t2>::internal_apply_visitor<boost::detail::variant::invoke_visitor<visitor>>(boost::detail::variant::invoke_visitor<visitor> &)' being compiled 1>          1>          [ 1>              t0_=std::vector<int>, 1>              t1=std::vector<double>, 1>              t2=std::vector<std::string>, 1>              visitor=const record_value_visitor<int> 1>          ] 1>          c:\boost_1_46_1\boost\variant\detail\apply_visitor_unary.hpp(76) : see reference function template instantiation 'void boost::variant<t0_,t1,t2>::apply_visitor<const visitor>(visitor &)' being compiled 1>          1>          [ 1>              t0_=std::vector<int>, 1>              t1=std::vector<double>, 1>              t2=std::vector<std::string>, 1>              visitor=record_value_visitor<int> 1>          ] 1>          c:\documents , settings\g88791\my documents\visual studio 2010\projects\test_boost\test_boost.cpp(58) : see reference function template instantiation 'void boost::apply_visitor<record_value_visitor<t>,boost::variant<t0_,t1,t2>>(const visitor &,visitable &)' being compiled 1>          1>          [ 1>              t=int, 1>              t0_=std::vector<int>, 1>              t1=std::vector<double>, 1>              t2=std::vector<std::string>, 1>              visitor=record_value_visitor<int>, 1>              visitable=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>> 1>          ] 1>c:\boost_1_46_1\boost\variant\variant.hpp(832): error c2664: 'void record_value_visitor<t>::operator ()(const std::vector<_ty> &) const' : cannot convert parameter 1 't2' 'const std::vector<_ty> &' 1>          1>          [ 1>              t=int, 1>              _ty=int 1>          ] 1>          , 1>          [ 1>              _ty=int 1>          ] 1>          reason: cannot convert 't2' 'const std::vector<_ty>' 1>          1>          [ 1>              _ty=int 1>          ] 1>          no user-defined-conversion operator available can perform conversion, or operator cannot called 1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(145) : see reference function template instantiation 'void boost::detail::variant::invoke_visitor<visitor>::internal_visit<t>(t &,int)' being compiled 1>          1>          [ 1>              visitor=const record_value_visitor<int>, 1>              t=t2 1>          ] 1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(173) : see reference function template instantiation 'void boost::detail::variant::visitation_impl_invoke_impl<visitor,voidptrcv,t>(int,visitor &,voidptrcv,t *,boost::mpl::false_)' being compiled 1>          1>          [ 1>              visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>, 1>              voidptrcv=void *, 1>              t=t2 1>          ] 1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(256) : see reference function template instantiation 'void boost::detail::variant::visitation_impl_invoke<visitor,voidptrcv,t2,nobackupflag>(int,visitor &,voidptrcv,t *,nobackupflag,int)' being compiled 1>          1>          [ 1>              visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>, 1>              voidptrcv=void *, 1>              nobackupflag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_, 1>              t=t2 1>          ] 1>c:\boost_1_46_1\boost\variant\variant.hpp(832): error c2664: 'void record_value_visitor<t>::operator ()(const std::vector<_ty> &) const' : cannot convert parameter 1 't0' 'const std::vector<_ty> &' 1>          1>          [ 1>              t=std::string, 1>              _ty=std::string 1>          ] 1>          , 1>          [ 1>              _ty=std::string 1>          ] 1>          reason: cannot convert 't0' 'const std::vector<_ty>' 1>          1>          [ 1>              _ty=std::string 1>          ] 1>          no user-defined-conversion operator available can perform conversion, or operator cannot called 1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(145) : see reference function template instantiation 'void boost::detail::variant::invoke_visitor<visitor>::internal_visit<t>(t &,int)' being compiled 1>          1>          [ 1>              visitor=const record_value_visitor<std::string>, 1>              t=t0 1>          ] 1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(173) : see reference function template instantiation 'void boost::detail::variant::visitation_impl_invoke_impl<visitor,voidptrcv,t>(int,visitor &,voidptrcv,t *,boost::mpl::false_)' being compiled 1>          1>          [ 1>              visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>, 1>              voidptrcv=void *, 1>              t=t0 1>          ] 1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(256) : see reference function template instantiation 'void boost::detail::variant::visitation_impl_invoke<visitor,voidptrcv,t0,nobackupflag>(int,visitor &,voidptrcv,t *,nobackupflag,int)' being compiled 1>          1>          [ 1>              visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>, 1>              voidptrcv=void *, 1>              nobackupflag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_, 1>              t=t0 1>          ] 1>          c:\boost_1_46_1\boost\variant\variant.hpp(1776) : see reference function template instantiation 'void boost::detail::variant::visitation_impl<first_which,first_step,visitor,voidptrcv,boost::variant<t0_,t1,t2>::has_fallback_type_>(const int,const int,visitor &,voidptrcv,boost::mpl::false_,nobackupflag,which *,step0 *)' being compiled 1>          1>          [ 1>              visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>, 1>              voidptrcv=void *, 1>              t0_=std::vector<int>, 1>              t1=std::vector<double>, 1>              t2=std::vector<std::string>, 1>              nobackupflag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_, 1>              which=first_which, 1>              step0=first_step 1>          ] 1>          c:\boost_1_46_1\boost\variant\variant.hpp(1787) : see reference function template instantiation 'void boost::variant<t0_,t1,t2>::internal_apply_visitor_impl<visitor,void*>(int,int,visitor &,voidptrcv)' being compiled 1>          1>          [ 1>              t0_=std::vector<int>, 1>              t1=std::vector<double>, 1>              t2=std::vector<std::string>, 1>              visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>, 1>              voidptrcv=void * 1>          ] 1>          c:\boost_1_46_1\boost\variant\variant.hpp(1810) : see reference function template instantiation 'void boost::variant<t0_,t1,t2>::internal_apply_visitor<boost::detail::variant::invoke_visitor<visitor>>(boost::detail::variant::invoke_visitor<visitor> &)' being compiled 1>          1>          [ 1>              t0_=std::vector<int>, 1>              t1=std::vector<double>, 1>              t2=std::vector<std::string>, 1>              visitor=const record_value_visitor<std::string> 1>          ] 1>          c:\boost_1_46_1\boost\variant\detail\apply_visitor_unary.hpp(76) : see reference function template instantiation 'void boost::variant<t0_,t1,t2>::apply_visitor<const visitor>(visitor &)' being compiled 1>          1>          [ 1>              t0_=std::vector<int>, 1>              t1=std::vector<double>, 1>              t2=std::vector<std::string>, 1>              visitor=record_value_visitor<std::string> 1>          ] 1>          c:\documents , settings\g88791\my documents\visual studio 2010\projects\test_boost\test_boost.cpp(63) : see reference function template instantiation 'void boost::apply_visitor<record_value_visitor<t>,boost::variant<t0_,t1,t2>>(const visitor &,visitable &)' being compiled 1>          1>          [ 1>              t=std::string, 1>              t0_=std::vector<int>, 1>              t1=std::vector<double>, 1>              t2=std::vector<std::string>, 1>              visitor=record_value_visitor<std::string>, 1>              visitable=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>> 1>          ] 1>c:\boost_1_46_1\boost\variant\variant.hpp(832): error c2664: 'void record_value_visitor<t>::operator ()(const std::vector<_ty> &) const' : cannot convert parameter 1 't1' 'const std::vector<_ty> &' 1>          1>          [ 1>              t=std::string, 1>              _ty=std::string 1>          ] 1>          , 1>          [ 1>              _ty=std::string 1>          ] 1>          reason: cannot convert 't1' 'const std::vector<_ty>' 1>          1>          [ 1>              _ty=std::string 1>          ] 1>          no user-defined-conversion operator available can perform conversion, or operator cannot called 1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(145) : see reference function template instantiation 'void boost::detail::variant::invoke_visitor<visitor>::internal_visit<t>(t &,int)' being compiled 1>          1>          [ 1>              visitor=const record_value_visitor<std::string>, 1>              t=t1 1>          ] 1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(173) : see reference function template instantiation 'void boost::detail::variant::visitation_impl_invoke_impl<visitor,voidptrcv,t>(int,visitor &,voidptrcv,t *,boost::mpl::false_)' being compiled 1>          1>          [ 1>              visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>, 1>              voidptrcv=void *, 1>              t=t1 1>          ] 1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(256) : see reference function template instantiation 'void boost::detail::variant::visitation_impl_invoke<visitor,voidptrcv,t1,nobackupflag>(int,visitor &,voidptrcv,t *,nobackupflag,int)' being compiled 1>          1>          [ 1>              visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>, 1>              voidptrcv=void *, 1>              nobackupflag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_, 1>              t=t1 1>          ] ========== build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

the following compiles , runs expected:

template<typename t> struct record_value_visitor : boost::static_visitor<> {     explicit record_value_visitor(t const& val) : val_(val) { }      void operator ()(std::vector<t>& vec) const     {         vec.push_back(this->val_);     }      template<typename u>     void operator ()(std::vector<u> const&) const { /* noop */ }  private:     t val_; };  template<typename t> record_value_visitor<t> make_record_value_visitor(t const& val) {     return record_value_visitor<t>(val); }  struct monitor {     explicit monitor(int const type) : type_(type)     {         switch (type)         {         case 1:             this->values_ = std::vector<int>();             break;         case 2:             this->values_ = std::vector<std::string>();             break;         default:             throw std::exception("invalid type");         }     }      void add_value(int const val)     {         if (this->type_ != 1)             throw std::runtime_error("invalid type add_value()");         boost::apply_visitor(make_record_value_visitor(val), this->values_);     }      void add_value(std::string const& val)     {         if (this->type_ != 2)             throw std::runtime_error("invalid type add_value()");         boost::apply_visitor(make_record_value_visitor(val), this->values_);     }  private:     int type_;     boost::variant<         std::vector<int>,         std::vector<double>,         std::vector<std::string>     > values_; };  int main() {     monitor int_mon(1);     int_mon.add_value(4);      monitor str_mon(2);     str_mon.add_value("foo"); } 

the visitor's operator() needs callable every possible type in variant – templated record_value_visitor::operator() key here.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -