C++ 表示 uint64_t 最大值的宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16298650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:14:24  来源:igfitidea点击:

Macro representing maximum value for uint64_t

c++uint64

提问by Subway

I'm seeking for a macro representing the maximum value of uint64_tas UINT_MAXis for unsigned int. i.e. I need this value to guaranteed to be (1<<64)-1.

我正在寻找一个表示uint64_tas UINT_MAXis for的最大值的宏unsigned int。即我需要这个值保证为 (1<<64)-1。

I tried to use UINT64_MAX, but compiling with g++ results in:

我尝试使用UINT64_MAX,但使用 g++ 编译会导致:

'UINT64_MAX' was not declared in this scope

It's worth to mention that I have this line #define __STDC_LIMIT_MACROSin the code before using UINT64_MAX.

值得一提的是,#define __STDC_LIMIT_MACROS在使用UINT64_MAX.

I was surprised to not find helpful information around the web about it.

我很惊讶在网上找不到有关它的有用信息。

采纳答案by pmr

Using the cstdintheader portably can be quite a challenge (it is missing from some MSVC implementations). At the same time numeric_limits::max()can be hard to use without constexprand it is not actually required to work with uint64_t. If you don't care about those things too much, std::numeric_limits<uint64_t>::max()will most likely do the trick.

cstdint可移植地使用标头可能是一个很大的挑战(某些 MSVC 实现中缺少它)。同时,numeric_limits::max()没有constexpr它可能很难使用,实际上也不需要使用uint64_t. 如果你不太关心这些事情,std::numeric_limits<uint64_t>::max()很可能会成功。

Boost.Integerhas an implementation of cstdintand comes with an extra traits class to get a constant maximal value. A compliant implementation of cstdintshould also provide the macro UINT64_MAX, but I'm not sure about boost.

Boost.Integer有一个实现cstdint并带有一个额外的traits 类来获得一个恒定的最大值。的合规实现也cstdint应该提供宏UINT64_MAX,但我不确定提升。