保存 std::byte 的 C++ 类

问题描述 投票:0回答:1

是否有任何 C++ 类可以保存到文件中

std::byte
s? 我已经使用了
std::ofstream
,但是需要演员来执行它:

std::byte bt{ 1 };
std::ofstream ofs{};
ofs.write(reinterpret_cast<const char*>(bt), 1);

你看到:

reinterpret_cast<const char*>
? 是否有其他 C++ 类可以在不进行任何强制转换的情况下保存
std::byte

c++ c++17
1个回答
0
投票

如果您不喜欢那个演员,您可以制作自己的

ofstream

using ofbytestream = std::basic_ofstream<std::byte>;

int main() {
    std::array<std::byte, 4> bytes;
    ofbytestream ofs("bytes");
    ofs.write(bytes.data(), bytes.size());
}
© www.soinside.com 2019 - 2024. All rights reserved.