Assign std::vector<std::byte> to std::vector<char> WITHOUT copying memory
I would probably leave the data in the original vector<byte>
and make a small class that keeps a reference to the original vector<byte>
and does the necessary casting when you need it.
Example:
#include <cstddef>
#include <iostream>
#include <vector>
template<typename T>
struct char_view {
explicit char_view(std::vector<T>& bytes) : bv(bytes) {}
char_view(const char_view&) = default;
char_view(char_view&&) = delete;
char_view& operator=(const char_view&) = delete;
char_view& operator=(char_view&&) = delete;
// capacity
size_t element_count() const { return bv.size(); }
size_t size() const { return element_count() * sizeof(T); }
// direct access
auto data() const { return reinterpret_cast<const char*>(bv.data()); }
auto data() { return reinterpret_cast<char*>(bv.data()); }
// element access
char operator[](size_t idx) const { return data()[idx]; }
char& operator[](size_t idx) { return data()[idx]; }
// iterators - with possibility to iterate over individual T elements
using iterator = char*;
using const_iterator = const char*;
const_iterator cbegin(size_t elem = 0) const { return data() + elem * sizeof(T); }
const_iterator cend(size_t elem) const { return data() + (elem + 1) * sizeof(T); }
const_iterator cend() const { return data() + size(); }
const_iterator begin(size_t elem = 0) const { return cbegin(elem); }
const_iterator end(size_t elem) const { return cend(elem); }
const_iterator end() const { return cend(); }
iterator begin(size_t elem = 0) { return data() + elem * sizeof(T); }
iterator end(size_t elem) { return data() + (elem + 1) * sizeof(T); }
iterator end() { return data() + size(); }
private:
std::vector<T>& bv;
};
int main() {
using std::byte;
std::vector<byte> byte_vector{byte{'a'}, byte{'b'}, byte{'c'}};
char_view cv(byte_vector);
for(char& ch : cv) {
std::cout << ch << '\n';
}
}
Output:
a
b
c
A simpler option if you only need const
access could be to create a string_view
:
template<typename T>
std::string_view to_string_view(const std::vector<T>& v) {
return {reinterpret_cast<const char*>(v.data()), v.size() * sizeof(T)};
}
//...
auto strv = to_string_view(byte_vector);
std::vector
does not allow attaching or detaching to memory allocations , other than moves from a vector of exactly the same type. This has been proposed but people raised (valid) objections about the allocator for attaching and so on.
The function returning vector<byte>
constrains you to work with a vector<byte>
as your data container unless you want to copy the data out.
Of course, you can alias the bytes as char
in-place for doing character operations.