Your problem cannot be solved without copying. When you store some floats into a std::vector<boost::any>
, those floats will be stored in memory at completely unrelated places. This is caused by the applied type eraseure technique.
Live demo: https://godbolt.org/z/a91qWazK7
You can observe that those floats have different addresses than are the addresses of any
objects inside a vector. This means that the floats are not stored in the vector's buffer itself. (In theory, they could be stored in this buffer if some small buffer optimization technique would be applied by boost::any
, which doesn't seem to happen. However, even then, this wouldn't help since the elements wouldn't be store contiguously in memory.)
Bottom line: If you need to get floats stored in std::vector<boost::any>
to std::vector<float>
, there is no way to do that without copying them.