using apply_visitor to filter from vector of variant

apply-visitor, boost, boost-variant, c++

Solution

struct T2_visitor : public boost::static_visitor<>
{
   T2_visitor(std::vector<T2>& v) : vec(v) {}
   template<typename T>
   void operator () (const T&) {}
   void operator () (const T2& value)
   {
      vec.push_back(value);
   }
private:
   std::vector<T2>& vec;
};

std::vector<T2> T2Vec;
T2_visitor vis(T2Vec);
std::for_each(Vec.begin(), Vec.end(), boost::apply_visitor(vis));

Problem

Yesterday I asked this question and "juanchopanza" answered my question, but unfortunately I cant caught one of bounded types. Since using a "visitor" is more robust, I'm also wondering of anyone could give me a solution using "visitor"? I am looking for the best way to filter a vector of the boost variant which has been defined like this: ``` boost::variant<T1*, T2, T3> Var; std::vector<Var> Vec; ``` when I call this vector, what is the best way to filter only T2 bounded type and insert into new vector? or in other way, I want something like this `std::vector<T2> T2Vec =` ...(how to filter it from Vec using apply_visitor)... thanks again! EDIT: the sulotion by @ ForEveR: ``` template<typename T> struct T_visitor : public boost::static_visitor<> { T_visitor(std::vector<T>& v) : vec(v) {} template<typename U> void operator () (const U&) {} void operator () (const T& value) { vec.push_back(value); } private: std::vector<T>& vec; }; ``` and: ``` std::vector<T1> t1vec; T_visitor<T1> vis(t1vec); std::for_each(vec.begin(), vec.end(), boost::apply_visitor(vis)); ``` would you please tell me what's wrong here?

Original source