문제

I have a use case where I'd like to process the element contained within a boost::variant without regard to its type.

Is there a way to get a pointer to the variant's data without knowing the element's type?

도움이 되었습니까?

해결책

You can write a visitor to do it:

 typedef boost::variant<T1,T2,T3> my_variant;
 void foo(my_variant v) {
      struct get_pointer: boost::static_visitor<void *> {
          template<class T>
          void *operator()(T &element) const 
          {
            return &element 
          }
      };
      get_pointer vis;
      void *data = boost::apply_visitor(vis, v);
 }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top