c++ const 使用。我可以删除它吗?

标签 c++ constants

我想实现一个快速随机生成器,我发现了这个网站:https://en.wikipedia.org/wiki/Xorshift ,建议使用以下代码

#include <stdint.h>

/* The state must be seeded so that it is not everywhere zero. */
uint64_t s[2];

uint64_t xorshift128plus(void) {
    uint64_t x = s[0];
    uint64_t const y = s[1];
    s[0] = y;
    x ^= x << 23; // a
    s[1] = x ^ y ^ (x >> 17) ^ (y >> 26); // b, c
    return s[1] + y;
}

我想知道这里的 const 是否有任何用处,我可以安全地删除它吗?

最佳答案

const这里防止y被意外修改;例如,如果程序员不小心打错了 x在第四条声明中为 y ( y ^= x << 23 ) 编译器会提示。

您可以删除它而不会对程序产生任何语义影响,但我不明白您为什么要这样做。

关于c++ const 使用。我可以删除它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39117291/

相关文章:

c++ - 在 tr1 中为模板类定义散列

c++ - C++ 中的签名保留装饰器模式?

C++通过指针对象键访问 map 元素给出非法操作错误

C++ const 双指针

c++ - 不明白为什么 const 限定符被丢弃

c++ - vector 和许多索引

c++ - 套接字:大消息性能

c++ - 使用来自 boost::math 的 Gauss-Kronrod 求积积分复杂函数

c++ - 关于传递给函数后指针重用的问题

c - 将 const 变量的成员分配给另一个 const