加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 严格别名冲突

发布时间:2020-12-16 03:43:15 所属栏目:百科 来源:网络整理
导读:以下程序是否违反严格别名规则? #include cstdintint main(){ double d = 0.1; //std::int64_t n = *reinterpret_caststd::int64_t*(d); // aliasing violation //auto n{*reinterpret_caststd::int64_t*(d)}; // aliasing violation auto nptr{reinterpret
以下程序是否违反严格别名规则?
#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,clang或gcc未发出警告.

解决方法

Does the following program violate the strict aliasing rule?

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

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

auto& n{*nptr};

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读