C++ 结构可以转换为 Microsoft Bond 结构吗?

Can C++ structs be casted to Microsoft Bond structs?

目标是将提供的 C++ 结构转换为债券结构。这是邦德支持的吗?

否则是否有任何好的示例可用于将 C++ 结构一般转换为已编译的键结构?我更愿意集中此逻辑,而不是为每个债券结构创建自定义转换。

如果您可以提供 bond::schema<T> for the non-Bond generated struct, that would give enough compile-time metadata about the non-Bond struct that many of the Bond APIs could be called on the non-Bond generated struct, like the transforms APIs. For example, serialization of std::tuple<T...> 的专业化,则通过专业化 bond::schema<std::tuple<T...>> 实现。

C++ 不--yet--have compile-time reflection for structs. There are various ways of simulating this (e.g., Boost::Fusion, macro-based schemes, code generation tools). A large part of Bond's C++ code generation is adding compile-time schema 库稍后使用。

如果 C++ 结构和 Bond 生成的 C++ 结构具有兼容的布局,并且两者都是 trivially copyable, you can use std::memcpy to "cast-by-copying" to the Bond-generated struct and then manipulate the Bond struct as needed. I'd wrap such behavior in a helper method with generous static_assert checks to avoid undefined behavior as the structs evolve. This will only work on very constrained struct shapes: collection fields, for example, will likely render the struct non-trivially copyable. You need to be using the 8.x or later version Bond and a recent version of your C++ compiler for the generated Bond structs to even have the ability to be trivially copyable at all. (See commit fa02289f,这删除了许多非默认的 ctors。)