Boost logo

Boost Users :

From: Hicham Mouline (hicham_at_[hidden])
Date: 2008-05-29 16:09:45


Hello,

I have a use of the visitor pattern (this was an answer provided to me on
c++-moderated) as shown below.

 

Question1: is the maximum number of variant<> arguments extendable?

Question2: Below, some of the variants I have are full template specs.

Is there a way to write the variant<> typedef with templates instead and
have

the operator() of the static visitor a template member.

 

Rds,

---------------

 

#include <iostream>

#include <boost/variant.hpp>

 

struct derived1 {};

struct derived2 {};

struct derived3 {};

 

typedef boost::variant<derived1, derived2, derived3> base;

 

// function object

struct operation : boost::static_visitor<int> {

   int operator()(const derived1&) const {

     return 1;

   }

   int operator()(const derived2&) const {

     return 2;

   }

   int operator()(const derived3&) const {

     return 3;

   }

};

 

int main() {

   base a = derived1();

   operation op;

   std::cout << a.apply_visitor(op) << std::endl; // prints "1"

   base a = derived2();

   std::cout << a.apply_visitor(op) << std::endl; // prints "2"

}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net