相关疑难解决方法(0)

为什么编译器不再使用严格的别名来优化此UB

谷歌严格别名的第一个结果之一就是这篇文章 http://dbp-consulting.com/tutorials/StrictAliasing.html
我注意到的一个有趣的事情是:http://goo.gl/lPtIa5

uint32_t swaphalves(uint32_t a) {
  uint32_t acopy = a;
  uint16_t* ptr = (uint16_t*)&acopy;
  uint16_t tmp = ptr[0];
  ptr[0] = ptr[1];
  ptr[1] = tmp;
  return acopy;
}
Run Code Online (Sandbox Code Playgroud)

被编译为

swaphalves(unsigned int):
        mov     eax, edi
        ret
Run Code Online (Sandbox Code Playgroud)

由GCC 4.4.7.任何比这更新的编译器(文章中提到的4.4所以文章没有错)都没有实现该功能,因为它可以使用严格别名.这是什么原因?它实际上是GCC中的错误还是GCC决定放弃它,因为许多行代码是以产生UB的方式编写的,或者它只是一个持续多年的编译器回归...而Clang也没有优化它.

c c++ gcc clang strict-aliasing

16
推荐指数
2
解决办法
1054
查看次数

标签 统计

c ×1

c++ ×1

clang ×1

gcc ×1

strict-aliasing ×1