Boost logo

Boost :

From: J.F.K. (beholder_at_[hidden])
Date: 2003-10-15 04:17:38


not "Char value: 'x'", but "float"

The following snip demonstrates the visitation facility using
a concrete visitor class, print_int_float_visitor. The code
will print "Char value: 'x'" and "Int value: 77" to std::cout:

    struct print_int_float_visitor
        : boost::static_visitor<void> // Return type is
void,
                                         // so derive from
static_visitor<void>
    {
       // Handle int values
       void operator()(int x) const
       {
          std::cout << "Int value: " << x << std::endl;
       }

       // Handle char values
       void operator()(float x) const
       {
          std::cout << "Float value: " << x << std::endl;
       }
    };
    .
    .
    variant<int,float> var = 53.22f;

    //Apply print_int_float_visitor to var.
    apply_visitor(print_int_float_visitor(), var); // Output:
"Float value: 53.22"

    var = 77;
    apply_visitor(print_int_float_visitor(), var); // Output:
"Int value: 77"

Note how print_int_char_visitor specifies its return type by
publicly deriving from boost::static_visitor<void>.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk