UINT32_MAX 的 C++ 等价物是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1471353/
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:06:07  来源:igfitidea点击:

What's the C++ equivalent of UINT32_MAX?

c++typesc99

提问by kdt

In C99, I include stdint.hand that gives me UINT32_MAXas well as uint32_tdata type. However, in C++ the UINT32_MAXgets defined out. I can define __STDC_LIMIT_MACROSbefore including stdint.h, but this does not work if someone is including my header after already including stdint.hthemselves.

在 C99 中,我包含stdint.h和 这给了我UINT32_MAX以及uint32_t数据类型。但是,在 C++ 中,UINT32_MAX定义了。我可以在 include__STDC_LIMIT_MACROS之前定义stdint.h,但是如果有人在已经包括stdint.h他们自己之后包括我的标题,这将不起作用。

So in C++, what is the standard way of finding out the maximum value representable in a uint32_t?

那么在 C++ 中,找出 a 中可表示的最大值的标准方法是uint32_t什么?

回答by Glen

Not sure about uint32_t, but for fundamental types (bool, char, signed char, unsigned char, wchar_t, short, unsigned short, int, unsigned int, long, unsigned long, float, doubleand long double) you can use the numeric_limitstemplates via #include <limits>.

不确定uint32_t,但对于基本类型(bool, char, signed char, unsigned char, wchar_t, short, unsigned short, int, unsigned int, long, unsigned long, float,doublelong double)您可以numeric_limits通过使用模板#include <limits>

cout << "Minimum value for int: " << numeric_limits<int>::min() << endl;
cout << "Maximum value for int: " << numeric_limits<int>::max() << endl;

If uint32_tis a #defineof one of the above than this code should work out of the box

如果uint32_t#define上述之一,则此代码应该开箱即用

cout << "Maximum value for uint32_t: " << numeric_limits<uint32_t>::max() << endl;

回答by Pete Kirkham

std::numeric_limits<T>::max()defines the maximum value for type T.

std::numeric_limits<T>::max()定义 type 的最大值T

回答by Lior Kogan

Well, uint32_t will always be 32 bit, and always be unsigned, so you can safely define it manually:

嗯, uint32_t 将始终是 32 位,并且始终是无符号的,因此您可以安全地手动定义它:

#define UINT32_MAX  (0xffffffff)

You can also do

你也可以这样做

#define UINT32_MAX  ((uint32_t)-1)

回答by bk1e

You may be able to eliminate the #includeorder problems by changing your build process to define the __STDC_LIMIT_MACROSsymbol on the compiler command line instead:

您可以#include通过更改构建过程以__STDC_LIMIT_MACROS在编译器命令行上定义符号来消除顺序问题:

cxx -D__STDC_LIMIT_MACROS ...

Of course, you would still have trouble if a header #undefs this symbol.

当然,如果标题#undef是这个符号,你仍然会遇到麻烦。

Also, the authors of the standard library implementation that you are using might not have intended for users to set that particular symbol; there might be a compiler flag or a different symbol that users are intended to use to enable C99 types in C++.

此外,您使用的标准库实现的作者可能不打算让用户设置该特定符号;可能存在编译器标志或不同的符号,用户打算使用它们来启用 C++ 中的 C99 类型。

回答by Robert Rodriguez

I can't comment so here is my input on Glen vs Lior Kogan's answer.

我无法发表评论,所以这是我对 Glen vs Lior Kogan 的回答的意见。

If you are using static variables you will run into the problem that if you assign a constant value inside a class to numeric_limits::max() that value will be in fact set to zero because of the order of initialization (see this post zero initialization and static initialization of local scope static variable)

如果您使用的是静态变量,您将遇到的问题是,如果您将类内的常量值分配给 numeric_limits::max(),由于初始化的顺序,该值实际上将设置为零(请参阅此帖子零初始化和局部范围静态变量的静态初始化

So in that case it will only work by using Lior Kogan's answer.

所以在这种情况下,它只能通过使用 Lior Kogan 的答案来工作。

// This looks cleaner, less error prone and easier to read than the other suggested by Lior Kogan
#define UINT32_MAX  ((uint32_t)-1)