How to move a std::vector into a raw array in C++
You can't.
A vector owns its buffer. You cannot steal it.
You will have to copy/move the elements individually, optionally using a helper algorithm that does the iteration for you (std::copy
/std::move
).
(Also note that, since your element type is just float
, a move here is a copy.)
(Also note that this std::move
, the algorithm, is not the same as std::move
, the rvalue cast.)
Consider whether you really need to do this. You can treat the vector's data as an array using vec.data()
whenever you need to, as long as you keep the vector alive. Surely that's better than sacrificing RAII?