微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 严格别名冲突

以下程序是否违反严格别名规则?
#include <cstdint>

int main()
{
    double d = 0.1;

    //std::int64_t n = *reinterpret_cast<std::int64_t*>(&d); // aliasing violation

    //auto n{*reinterpret_cast<std::int64_t*>(&d)}; // aliasing violation

    auto nptr{reinterpret_cast<std::int64_t*>(&d)};
    auto& n{*nptr};

    ++n;
}

VS2015,clanggcc未发出警告.

解决方法

Does the following program violate the strict aliasing rule?

是的,它确实.您正在使用std :: int64_t *取消引用double *(& d).

违反严格别名规则的行是:

auto& n{*nptr};

在处理行时,编译器不一定知道如何设置nptr的值.在处理该行时,它是double *的别名这一事实并不明显.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐