【发布时间】:2018-03-06 19:12:32
【问题描述】:
我正在尝试调用这个函数
static inline void insert(Buffer *buf, double f, size_t index)
{
insert_64(buf, *(uint64_t *)&f, index);
}
但是,我收到了这个错误:
error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
不知道我哪里错了。
【问题讨论】:
-
*(uint64_t *)&f那里。你不能假装指向float的指针是指向uint64_t的指针并侥幸逃脱。 -
@LeeDanielCrocker 取消引用新指针是 UB,OP 肯定做错了什么。
-
不,这不仅仅是关于便携性。打破严格的别名规则以及一些优化可能会导致更有趣的东西。看这里:blog.qt.io/blog/2011/06/10/type-punning-and-strict-aliasing
标签: c