c++ - 在字节数组中连接结构
问题描述
我想连接两个结构。问题是我无法将该 struct 转换为 a std::span
,因为 span 的构造函数不支持void*
. 我也使用相同的 concat 函数进行连接std::vector<uint8_t>
,因此对该函数所做的任何更改都不应该影响它。如果可以将 struct 转换为vector<uint8_t>
,我将能够更改std::span<uint8_t>
为相同的向量。我怎样才能做到这一点?
address_t address{};
memcpy(address.country, country, sizeof country);
memcpy(address.city, city, sizeof city);
memcpy(address.state, state, sizeof state);
address.zip_code = zip_code;
...
const auto& a = reinterpret_cast<void*>(&address);
auto c = utils::concat(a, b);
片段
__declspec(align(16))
typedef struct
{
std::wchar_t country[30];
std::wchar_t city[50];
std::wchar_t state[50];
std::uint32_t zip_code;
} address_t;
namespace utils
{
inline std::vector<std::uint8_t> concat(const std::span<std::uint8_t>& a)
{
return std::vector<std::uint8_t>(a.data(), a.data() + a.size());
}
template <typename... Args>
std::vector<std::uint8_t> concat(const std::span<std::uint8_t>& a, Args&... args)
{
auto vec = std::vector<std::uint8_t>(a.data(), a.data() + a.size());
(vec.insert(vec.end(), args.begin(), args.end()), ...);
return vec;
}
}
解决方案
您可以提供一种将 an 重新解释address
为跨度的方法:
auto to_span(address const& a) -> std::span<uint8_t const> {
return {(uint8_t const*)&a, sizeof(a)};
}
然后你的其他东西就可以工作了:
auto c = utils::concat(to_span(address), to_span(other_address));
这可以概括为支持可简单复制的类型。
您concat
当前将参数作为const std::span<std::uint8_t>&
. 您通常希望span
按值获取(它是一种小型、可轻松复制的类型,您几乎从不关心 的身份)span
。此外,当您不修改的内容时span
(因为您不在这里),您要确保它是span
过度const
数据。
也就是说,您的参数应该具有 type std::span<uint8_t const>
。
这可以确保您不会通过修改span
(这很重要,并且允许您传递不可变的东西,就像我to_span
上面的那样),而接受const&
只会保护您不修改span
本身(这根本不重要)。
推荐阅读
- postgresql - repmgr - 从失败的主服务器中恢复?
- python - Django:ORM 设计问题
- python - 将复杂/扁平化的 JSON 转换为 DataFrame
- c# - 如何将占位符或 0 添加到 Blazor 日期
- node.js - 如何在 Firebase 云函数中进行 API 调用?
- java - 关闭 JFrame 后清空 ArrayList
- go - 去测试错误 - 没有测试文件 - GOPATH 中的错误
- odoo-11 - Odoo11 用自定义模块替换 sale.report_invoice_layouted 中的字段
- vue.js - VueJs - 使用数组中的数据创建所有周的卡片
- graphql - 第三方 API 的 Gatsby 插件抛出错误