c++boostapply-visitor

What's the return type of boost::apply_visitor (delayed version)


In the following code I have stored the result of delayed apply_visitor in an auto variable. What type can I use instead of auto? Is it possible to use std::function?

#include "boost/variant/variant.hpp"
#include "boost/variant/apply_visitor.hpp"

using namespace std;
using namespace boost;

class times_two_generic
    : public boost::static_visitor<>
{
public:

    template <typename T>
    void operator()( T & operand ) const
    {
        operand += operand;
    }

};

int main(int argc, char **argv) 
{
    variant<int, string> v = 5;
    times_two_generic visitor;

    cout << v << endl;

    apply_visitor(visitor)(v); // v => 10
    auto appliedVisitor = apply_visitor(visitor);
    appliedVisitor(v); // v => 20

    cout << v << endl;
    return 0;
}

Solution

  • According to http://www.boost.org/doc/libs/1_52_0/doc/html/boost/apply_visitor.html, the actual type is apply_visitor_delayed_t<times_two_generic>.

    Since it is just another function object, you could also use std::function, but that would cost more than using the real type.