在下面的代码中,我将延迟的结果存储apply_visitor
在一个auto
变量中。我可以用什么类型代替auto
?可以使用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;
}