How to input int64_t / uint64_t constants?(如何输入 int64_t/uint64_t 常量?)
问题描述
我想要做的是定义一个等于 2^30 的常量(我可能会将其更改为 2^34 之类的值,因此我更喜欢为它设置一个大于 32 位的房间).
What I'm trying to do is to define a constant equal to 2^30 (I may change it to something like 2^34, so I prefer to have a room larger than 32 bits for it).
为什么下面的最小(?)示例无法编译?
Why the following minimal(?) example doesn't compile?
#include <stdint.h>
// test.cpp:4:33: error: expected primary-expression before numeric constant
// test.cpp:4:33: error: expected ')' before numeric constant
const uint64_t test = (uint64_t 1) << 30;
//const uint64_t test1 = (uint64_t(1)) << 30;// this one magically compiles! why?
int main() { return 0; }
推荐答案
(uint64_t 1)
是无效的语法.转换时,您可以使用 uint64_t(1)
或 (uint64_t) 1
.注释掉的示例之所以有效,是因为它遵循正确的强制转换语法,就像这样:
(uint64_t 1)
is not valid syntax. When casting, you can either use uint64_t(1)
or (uint64_t) 1
. The commented out example works because it follows the proper syntax for casting, as would:
const uint64_t test = ((uint64_t)1) << 30;
虽然这直接回答了问题,但请参阅 Shafik Yaghmour 的回答,了解如何正确定义积分常数具体尺寸.
While this directly answers the question, see the answer by Shafik Yaghmour on how to properly define an integral constant with specific size.
这篇关于如何输入 int64_t/uint64_t 常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何输入 int64_t/uint64_t 常量?
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 近似搜索的工作原理 2021-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01