Boost logo

Boost :

From: Hartmut Kaiser (hartmutkaiser_at_[hidden])
Date: 2004-12-08 10:01:58


Hi all,

is there a means of using the binary visitation pattern provided by the
Boost.Variant library and make the return type of the visitation process
dependent on the types currently stored inside the visited variant object
instances?

I.e. I'm looking for some way to do the following:

    typedef boost::variant<unsigned long, long> variant_type;

    struct add_visitor : public ....
    {
        long operator()(long t1, long t2)
        {
            return t1 + t2;
        }
        template <typename T>
        unsigned long operator()(unsigned long t1, T t2)
        {
            return t1 + t2;
        }
        template <typename T>
        unsigned long operator()(T t1, unsigned long t2)
        {
            return t1 + t2;
        }
    };

    variant_type term1 = 1L;
    variant_type term2 = 2UL;
    variant_type term3 = -2L;

    variant_type unsigned_result =
        boost::apply_visitor(add_visitor(), term1, term2);
    variant_type signed_result =
        boost::apply_visitor(add_visitor(), term1, term3);

Is this possible with the current implementation?

Regards Hartmut


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