6

我有一个返回一个函数std::vector<std::byte>

我知道这std::byte不是字符类型也不是整数类型,并且只能通过类型转换将其转换为 char。到目前为止,一切都很好。

所以我想(在我知道向量只包含字符数据的情况下)将底层缓冲区的所有权从std::vector<std::byte>a 转移到std::vector<char>using std::move,以避免复制整个底层缓冲区。

当我尝试这样做时,我收到此错误:

不存在从“std::vector<std::byte, std::allocatorsstd::byte>”到“std::vector<char,std::allocator>”的合适的用户定义转换

这完全可能使用 C++ 吗?我认为有真正的用例想要这样做

4

3 回答 3

7

我可能会将数据保留在原始数据中,vector<byte>并创建一个小类来保留对原始数据的引用,vector<byte>并在需要时进行必要的转换。

例子:

#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';
    }
}

输出:

a
b
c

如果您只需要const访问权限,一个更简单的选择可能是创建一个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);
于 2020-06-21T23:35:09.813 回答
2

std::vector不允许附加或分离内存分配,除了从完全相同类型的向量移动。这已经被提出,但人们对附加分配器等提出了(有效的)反对意见。

除非您想将数据复制出来,否则返回的函数vector<byte>会限制您使用 a作为数据容器。vector<byte>

当然,您可以将字节别名为char就地进行字符操作。

于 2020-06-22T02:59:38.597 回答
-6

You can achieve this with a cast, as shown below. This is legal because the cast is to a char reference (if casting to any other type it would be UB) but, with gcc at least, you still have to compile it with -fno-strict-aliasing to silence the compiler warning. Anyway, here's the cast:

std::vector <char> char_vector = reinterpret_cast <std::vector <char> &&> (byte_vector);

And here's a live demo

于 2020-06-21T23:02:12.847 回答