Boost logo

Boost Users :

Subject: [Boost-users] Template type questions
From: Glenn Macgregor (gmacgregor_at_[hidden])
Date: 2009-12-17 11:39:57


Hi All,

I am using the boost::variant with the binary visitor algorithm, apply_visitor.

struct UnknownValue {};
typedef boost::variant<UnknownValue, long, unsigned long, double, string, char> ValueType;

I am using apply_visitor for binary visitation as below:

class AddVisitor : public boost::static_visitor<> {
public:
                template <typename T>
                void operator()(const T &lhs, const T &rhs) {
                                T val = lhs + rhs;
                }

                template <typename T, typename R>
                void operator()(const T &lhs, const R &rhs) {
                                // do nothing...different types
                }

                template <typename T>
                void operator()(const T &lhs, const UnknownValue &rhs) {
                                // do nothing...we have an unknown value
}

template <typename T>
                void operator()(const UnknownValue &lhs, const T &rhs) {
                                // do nothing...we have an unknown value
}

void operator()(const UnknownValue &lhs, const UnknownValue &rhs) {
                                // do nothing...we have an unknown value
}
};

The above is not complete because I can do addition if the types are long and unsigned long or long and double or long and char...etc.

What is the preferred method for doing this? Do I need to create an overridden operator() for every argument combination that is valid or can I get the argument types in the function?

Thanks

 Glenn



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