Best/proper 定义常量的方法 uint64_t

Best/proper way to define uint64_t constants

constexpr auto v = static_cast<std::uint64_t>(1) << 32; 并不理想,因为繁琐的语法和语义间接的转换。来自 this thread, I learned constexpr auto v = UINT64_C(1) << 32; However, the precise semantics of the macro

expands to an integer constant expression having the value specified by its argument and the type uint_least64_t.

因此,不完全是 uint64_t。我想知道定义 uint64_t 常量的 best/proper 方法是什么。

请注意 unsigned long long 不一定映射到 uint64_t

更新

我不愿意使用 functional/C-style 转换,因为一些(例如,Google C++ 编码风格)说它来自 C,现代 C++ 应该避免使用它们。看来我应该对此有自己的看法,而不是盲目跟风。

有说明符,使用 L 使您的整数文字成为 longLL 成为 long longULL 成为 unsigned long long,或者,uint64_t(大部分时间)。

或者只是将 auto 放在一起,让编译器进行隐式转换。

Therefore, it's not exactly uint64_t. I'm wondering what is the best/proper way to define uint64_t constants.

如果你想绝对确定,你可以写 uint64_t(1)(可能 std::-qualified)作为 static_cast.

的更短的替代方法

出于实际目的,UINT64_C 也可以。如果实现不提供满足其要求的任何类型,则不需要定义 uint64_t。因此根据 uint_least64_t.

定义 UINT64_C 是有道理的

但是在 uint64_t 确实存在的实现上——并且你已经隐含地假设你正在使用这样的实现——uint64_tuint_least64_t 必须具有完全相同的范围,是否几乎需要实现者的恶意才能不使它们成为完全相同的类型。

P.S。由于它被标记为 language-lawyer:无法保证 uint64_t(1) << 32 仍然具有类型 uint64_t。假设地,实现可以提供 larger-than-64 位 int 类型,在这种情况下 uint64_t 将提升为 int.