Oop! sorry, I did not intend to send that yet -- I was toying with the problem discussed in this thread but my finger slipped.. :(


From: John Femiani
Sent: Wednesday, May 07, 2008 2:53 AM
To: 'boost-users@lists.boost.org'
Subject: RE: [Boost-users] [variant] how to get 1st bounded type


> Why don't you like visitor to dispatch the type stored in the variant?
 
I like it very much, and I use variant because of the power of the static visiting mechanism.
The reason I wanted to test if the variant contains its 1st type is the following: my variant is made of a list of shared-pointers to various types; there's a routine that initializes it with one of the types depending on some conditions, then it applies some visitor on the resulting variant and stores it then in a container - however, there's no "default" behavior, so if neither condition was met, the variant remains "uninitialized" - i.e. it contains 1st type "zero" ptr. I want neither apply the visitor on "empty" smartpointer nor store it in the container, so I have to know whether it was "really" created or not. Two simple ways I can think about are to test the resulting variant or to introduce some flag - of course, both ways are far from ideal...
 
 
struct my_visitor {
    typedef bool return_type;
    vector<variant_type> & vec_;
 
    my_visitor (vector<variant_type> & vec) : vec_(vec) {}
    my_visitor (my_visitor  & other) : vec_(other.vec_) {}
 
    bool opeator()(...) {return false;}
    bool operator()(type1  value) { vec_.push_back(variant_type(value));  return true;  }
    bool operator()(type2  value) { vec_.push_back(variant_type(value));  return true; }
} 
 
 vector<variant_type>  my_vec 
:boost::apply_visitor(my_visitor(my_vec), my_variant)