对于结构,其中相等意味着每个数据成员的最大派生类型和字节相等,如果有的话,结构是否可以安全地作为字节数组进行哈希处理?
这个文件
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3333.html
在标题"将对象作为字节数组散列"下,建议使用任何填充的结构不能作为字节数组安全地进行散列.
是否需要将结构安全地散列为字节数组的填充的显式测试?这够了吗?
如果是这样,下面的草图是否恰当地说明了该测试?
#include <cstddef>
#include <iostream>
struct A
{
int i;
float f;
char c;
};
// hashing would start at offs_i (possibly hopping over a v-table) and end at
// offs_c + sizeof(char)
int main()
{
const std::size_t size_A = sizeof(A);
const std::size_t size_int = sizeof(int);
const std::size_t size_float = sizeof(float);
const std::size_t size_char = sizeof(char);
const std::size_t offs_i = offsetof(A, i);
const std::size_t offs_f = offsetof(A, f);
const std::size_t offs_c = offsetof(A, c);
bool …Run Code Online (Sandbox Code Playgroud)