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 常量?
基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 我有静态或动态 boost 库吗? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
