I have a function that returns a std::vector<std::byte>
I am aware that std::byte is not a character type nor an integral type, and that converting it to char is only possible through a typecast. So far so good.
So I would like (in cases where I know that the vector only contains character data) to transfer ownership of the underlying buffer from the std::vector<std::byte> to a std::vector<char> using std::move, so as to avoid copying the entire underlying buffer.
When I try doing this, I get this error:
no suitable user-defined conversion from "std::vector<std::byte, std::allocatorstd::byte>" to "std::vector<char,std::allocator>" exists
Is this at all possible using C++? I think there are real use cases where one would want to do this
std::bytes, hence you can't move them as new types. moving means transferring the ownership i.e the elements themselves will be moved, so If you can change the type of some object (without copying), you can do what you want.std::vectorlike type that has the ability to adopt external memory. Or consider std::span.vector<T>doesn't store memory; it stores an array ofTs. It is reasonable to allow avector<T>instance to adopt the storage from anothervector<T>instance. It makes far less sense for it to be able to adopt the "memory" of some unrelated typevector<U>, since that is an array ofUs, which is not an array ofTs.