Dear list,
I have a problem with apply_visitor of boost::variant. It seems to me
that it can not accept a temporary variant variable that is returned
from a function. Is this a bug?
Here is a simple example:
===================================
#include "boost/variant.hpp"
using namespace std;
#include <string>
typedef boost::variant Variable;
#include <iostream>
// a simple visitor that returns a variant.
class variantPlusOne
: public boost::static_visitor<Variable>
{
public:
Variable operator()( double i) const
{
return Variable( i + 1 );
}
Variable operator()( string str) const
{
return Variable( 0);
}
};
// apply_visitor in function form
Variable func(Variable a)
{
return boost::apply_visitor( variantPlusOne(), a);
}
// a silly function.
Variable me(Variable a)
{
return a;
}
int
main()
{
Variable b("5");
cout << func(b) << endl; // OK
cout << func(me(b)) << endl; // OK
cout << boost::apply_visitor(variantPlusOne(), b ) << endl; // OK
// cout << boost::apply_visitor(variantPlusOne(), me(b) ) << endl; // NO
return 0;
}
/////////////////////////////
The error messages are:
test.cpp:44: no matching function for call to `apply_visitor(variantPlusOne,
Variable)'
boost_1_31_0/boost/variant/detail/apply_visitor_unary.hpp:59: candidates are:
Visitor::result_type boost::apply_visitor(Visitor&, Visitable&) [with
Visitor = variantPlusOne, Visitable = Variable]
boost_1_31_0/boost/variant/detail/apply_visitor_unary.hpp:75:
Visitor::result_type boost::apply_visitor(const Visitor&, Visitable&) [with
Visitor = variantPlusOne, Visitable = Variable]
///////////////////////////////
Note that I can understand this behavior if the visitor takes
reference arguments (and therefore expects a persistent variable) but
my visitor pass argument by values.
Thanks.
Bo