Boost.Variant: Passing visitor as a function argument

Using the Boost.Variant library, I'm trying to pass a visitor as an argument to a class method (the class instance contains the objects to be visited), but I can't come up with a solution that compiles. For example: typedef std::pair<std::string, double> AttrDouble; typedef std::pair<std::string, int> AttrInt; typedef std::pair<std::string, std::string> AttrString; typedef boost::variant<AttrDouble, AttrInt, AttrString> Attr; typedef std::vector<Attr> AttrList; class Example { public: // Some code omitted for brevity -- void visit(boost::static_visitor<> const& visitor) { BOOST_FOREACH(Attr attr, i_attrList) { boost::apply_visitor(visitor, attr); } } private: AttrList i_attrList; }; The above gives compiler errors like this: boost-1_37/boost/variant/variant.hpp:806: error: no match for call to '(const boost::static_visitor<void>) (std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>&)' boost-1_37/boost/variant/variant.hpp:806: error: return-statement with a value, in function returning 'void' The problem seems to be that static_visitor doesn't actually contain op()--only the user-supplied derived class does. I've read through the Boost.Variant documentation repeatedly, looked at Boost.Variant source, and tried searching the Internet for examples, but I haven't found anything that shows how to do this, leading me to believe it may not be possible. Is there a way to pass a visitor as a function argument, or some alternative approach that allows visiting objects without having direct access to them (such as when they are class private data and the visitor is external to the class)? Thanks, Steve Hawkes

void visit(boost::static_visitor<> const& visitor) { BOOST_FOREACH(Attr attr, i_attrList) { boost::apply_visitor(visitor, attr); } }
You can define a template method: template<class Visitor> void visit(Visitor visitor) { BOOST_FOREACH(Attr attr, i_attrList) { boost::apply_visitor(visitor, attr); } }

void visit(boost::static_visitor<> const& visitor) { BOOST_FOREACH(Attr attr, i_attrList) { boost::apply_visitor(visitor, attr); } }
Just for note, but considering that you are asking for a static_vistor directly, and you create a visitor by using the CRTP, this shows that you might want to learn a little bit more about how C++ handles classes before delving into too much more.

void visit(boost::static_visitor<> const& visitor) { BOOST_FOREACH(Attr attr, i_attrList) { boost::apply_visitor(visitor, attr); } }
Just for note, but considering that you are asking for a static_vistor directly, and you create a visitor by using the CRTP, this shows that you might want to learn a little bit more about how C++ handles classes before delving into too much more.
Could you explain what you mean? If by CRTP you mean the "curiously recurring template pattern," how does that apply here? The template parameter for static_visitor is its return type, not the type of the derived visitor. What specifically should I be focusing on learning more about here?

On Fri, Sep 4, 2009 at 12:54 PM, Hawkes Steve-FSH016<Steve.Hawkes@motorola.com> wrote:
void visit(boost::static_visitor<> const& visitor) { BOOST_FOREACH(Attr attr, i_attrList) { boost::apply_visitor(visitor, attr); } }
Just for note, but considering that you are asking for a static_vistor directly, and you create a visitor by using the CRTP, this shows that you might want to learn a little bit more about how C++ handles classes before delving into too much more.
Could you explain what you mean? If by CRTP you mean the "curiously recurring template pattern," how does that apply here? The template parameter for static_visitor is its return type, not the type of the derived visitor. What specifically should I be focusing on learning more about here?
Er, right, it is not using it, brain-fart... Regardless, you need a fully declared type, void visit(boost::static_visitor<> const& visitor) will error as you saw. You need to templatize it as the post above mine shows: template<class Visitor> void visit(Visitor visitor)
participants (3)
-
Hawkes Steve-FSH016
-
Igor R
-
OvermindDL1