【发布时间】:2013-02-10 14:26:39
【问题描述】:
将 64 位常量赋值为
int64_t foo = 0x1234LL;
不可移植,因为long long 不一定是int64_t。这篇帖子Which initializer is appropriate for an int64_t? 讨论了<stdint.h> 中INT64_C() 宏的使用,但是否也可以将static_cast 用作
int64_t foo = static_cast<int64_t>(0x1234);
?
我应该更喜欢哪一个,为什么,或者它们都运作良好?
我在 Internet 和 SO 上进行了搜索,但没有找到任何探索 static_cast 选项的地方。我还使用sizeof() 进行了测试,以确认它在简单的情况下有效。
【问题讨论】:
-
如果常量大于 64 位,静态转换不会做错事,什么都不说?
标签: c++ static-cast int64