views:

63

answers:

2
  struct A
  {
    std::string get_string();
  }; 

  struct B
  {
    int value;
  }; 

  typedef boost::variant<A,B> var_types;
  std::vector<var_types> v;

  A a;
  B b;

  v.push_back(a);
  v.push_back(b);

How can I iterate iterate through the elements of v to get access to the a and b objects ?

I'm able to do it with boost::get but the syntax is really cumbersome.:

std::vector<var_types>:: it = v.begin();
while(it != v.end())
{
    if(A* temp_object = boost::get<A>(&(*it)))    
      std::cout << temp_object->get_string();
    ++it;
}

I've tried to use the visitation technique but I didn't get too far and the code is not working :

template<typename Type>
class get_object
  : public boost::static_visitor<Type>
{
public:

  Type operator()(Type & i) const
  {
    return i;
  }

};

...

while(it != v.end())
{
    A temp_object = boost::apply_visitor(get_object<A>(),*it);
    ++it;
}

EDIT 1

A hackish solution is :

class get_object
  : public boost::static_visitor<A>
{
public:

  A operator()(const A & i) const
  {
    return i;
  }

  A operator()(const B& i) const
  {
    return A();
  }        
};
+1  A: 

Edit:
If it's as UncleBens suggests, then you can simply do this:

BOOST_FOREACH(var_types& vt, v) {
    if (vt.which() == 0)  
        cout << get<A>(vt).get_string() << endl;
}

Original:
The template parameter for static_vistor is the return type of its methods. This means the two classes need to share a common return type for a single visitor. It should look something like this:

class ABVisitor : public static_visitor<string> {
public:
    string operator()(const A& a) const {
        return a.get_string();
    }
    string operator()(const B& b) const {
        return lexical_cast<string>(b.value);
    }
};

Here's an example of iteration using this visitor.

BOOST_FOREACH(var_types& vt, v)
    cout << apply_visitor(ABVisitor(), vt) << endl;
Gunslinger47
But doesn't this always output something, whereas in the original attempts, something was printed only if the object was A?
UncleBens
@UncleBens: you're right. The whole body of the for loop ought to be in the visitor this time.
Matthieu M.
As I said, I know how to do it with get but find the syntax very unfriendly if I have to expose it through a class interface.
anno
Sorry, that's as clean as it's going to get without using custom wrappers. For example, I just wrote one that works like this: `A* a = jam(vt);` The jam function returns a wrapper class with implicit conversions to pointers and references for the types deduced from the supplied variant.
Gunslinger47
Ok, I give up :)
anno
+3  A: 

As far as I can see, the visitor is supposed to do the work. If you just want to get the stored value, then boost::get - if I'm not mistaken - is the pre-made visitor for that.

Example:

  struct print_if_a : boost::static_visitor<void>
  {
    void operator()(A& a) const { std::cout << a.get_string() << '\n'; } //alas, get_string is non-const 
    void operator()(const B&) const {} //does nothing, up to optimizer
  };

Usage:

  BOOST_FOREACH(var_types& obj, v) {
    boost::apply_visitor(print_if_a(), obj);
  }
UncleBens
It is not necessary to precise the return type of `boost::static_visitor` here, it defaults to `void` if I am not mistaken.
Matthieu M.
@Matthieu: Yes, you could also write `boost::static_visitor<>`.
UncleBens