Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 256
B Faley Avatar asked Nov 30 '25 18:11

B Faley


1 Answers

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.

like image 198
kennytm Avatar answered Dec 03 '25 10:12

kennytm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!