|
Boost Users : |
From: Benjamin Winkler (Benjamin.Winkler_at_[hidden])
Date: 2007-02-26 14:21:09
References can not be copied. Thus, you can't make boost::any contain a reference (which is what mean when you write boost::any<double&>, I assume).
In your example, msgargs.push_back(dref) calls the any-constructor any(const T&) with T being double, and this copies the value, not the reference itself.
When you call msgargs.push_back(dptr), it calls the any(const T&) with T being double*. The value is copied again, only this time, the value is a pointer.
Benjamin Winkler
>Hi all,
>
>I've been trying out boost::any in a messaging system to package up function arguments. A message
>call is basically
>
>Message::Call(std::vector<boost::any>& args)
>{
>try
>...a few any_cast<..>
>...fire the actual function...
>catch
>...etc
>}
>
>It's all good, except for one niggling problem. Say I have a double-precision value that I need
>changed inside the message, so it needs to be passed as a double* or a double& in an argument. The
>problem is, if I create a double& inside a boost::any, it doesn't point to the right memory location
>once I'm inside the message, and it looks like it's copying the original dereferenced value and
>giving me a reference to the copy. If I create a double* inside a boost::any, I get a new pointer
>inside the message like the reference, but because the pointer target is correct everything works.
>The code looks like this:
>
>...
>...
>std::vector<boost::any> args;
>double d = 1.5;
>double* dptr = &d;
>msgargs.push_back(dptr);//works fine...
>double& dref = d;
>msgargs.push_back(dref);//seems to copy d instead of a reference to d...
>...
>msg->Call(args);
>...
>
>I can live with this, I just have to pass "large" objects by pointer so they don't get copied. BUT:
> Can anyone tell me why references are creating a copy of the original and not a copy of the
>reference, or how I could avoid the copying? Or am I doing something dumb?
>
>Damien
>_______________________________________________
>Boost-users mailing list
>Boost-users_at_[hidden]
>http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net