I am trying to create a boost::any from a reference to an object and change it inside a given method after a boost::any_cast. But the object stays unaltered after the call. Here is a simple example of what I am trying:
class Base
{
public:
template<typename T>
void method(T& data)
{
methodImpl(boost::any(data));
}
protected:
virtual void methodImpl(boost::any& data) = 0;
};
class Derived : public Base
{
private:
void methodImpl(boost::any& data)
{
Parameter& param = boost::any_cast<Parameter&>(data);
// Change param attributes...
}
};
int main()
{
Derived derived;
Parameter param;
derived.method(param);
// param hasn't changed...
}
Is there a simple way of making it work or do I have to use boost::ref and boost::reference_wrapper?
Based on the documentation of boost::any, it will always make a copy of the contents that you provide it:
http://www.boost.org/doc/libs/1_58_0/doc/html/boost/any.html
See item 4 and 8 in the "Description" section for documentation of the relevant constructor and assignment operator.