0

是否可以将返回值存储boost::apply_visitor在类的成员变量中?
我需要让Test::Do功能正常工作,但不知道如何。

#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;
    }

};

class Test
{
public:
   Test(){
      times_two_generic visitor;
      //mAppliedVisitor = apply_visitor(visitor);
   }
   ~Test(){}
   void Do(variant<int, string> &v)
   {
      //mAppliedVisitor(v);   // Is it possible to store the value of apply_visitor 
                           // in mAppliedVisitor only once in the constructor of Test? 
                           // and only call mAppliedVisitor member whenever needed.
   }

private:
   // boost::apply_visitor_delayed_t<times_two_generic> mAppliedVisitor;
};

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

    cout << v << endl;

    apply_visitor(visitor)(v); // v => 10
    boost::apply_visitor_delayed_t<times_two_generic> appliedVisitor = apply_visitor(visitor);
    appliedVisitor(v); // v => 20

   Test t;
   t.Do(v);

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

0 回答 0