It took me a while to figure this out, but the semantics of boost::any
are confusing.
With value types, you use it like so:
int value = 100;
boost::any something;
something = value;
//...later...
int value = boost::any_cast<int>(&something);
This code is clear and makes sense, but stores value
internally as a copy. This means for larger objects I place inside boost::any
, they will be copied. Also any functions that I replace void*
with this will expect that the value outside the function is modified when I modify the value contained in the boost::any
object (which won't happen, since it copied it).
So if I put pointers into it, things get weird:
int value = 100;
boost::any something;
something = &value;
//...later...
int* value = *boost::any_cast<int*>(&something);
I have to dereference the return value in this case because boost::any_cast
returns int**
! I also haven't checked but I think this may crash if something.empty() == true
. This is just not straightforward at all.
I do not want to store values in my boost::any
, I want it to only function on pointers and behave semantically closer to void*
. Pointers in, pointers out, with some type-safety mixed in. Essentially what I want is boost::any_pointer
, or something like that. Is there a way to prohibit boost::any
from accepting anything except pointers? And if not, is there an alternative to boost::any
that can give me the semantics I'm looking for?