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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With