constexpr auto v = static_cast<std::uint64_t>(1) << 32;
并不是最理想的,因为其语法繁琐且类型转换间接。从这个线程中,我学到了constexpr auto v = UINT64_C(1) << 32;
然而,宏的精确语义是:
展开为一个整数常量表达式,具有由其参数指定的值和类型
uint_least64_t
。
因此,它并不完全等同于uint64_t
。我想知道定义uint64_t
常量的最佳/适当方式是什么。
请注意,unsigned long long
不一定映射到uint64_t
。
更新
我不愿使用函数式/C风格转换,因为有些人(例如Google C++编码风格)认为它们来自C,在现代C ++中应避免使用它们。看起来我应该有自己的观点,而不是盲目地跟随别人。
constexpr auto v = std::uint64_t{1} << 32
?这与Herb Sutter的“几乎总是auto”建议相辅相成。 - StoryTeller - Unslander Monica