What's the C++ equivalent of UINT32_MAX?

前端 未结 5 1826
广开言路
广开言路 2021-02-02 07:27

In C99, I include stdint.h and that gives me UINT32_MAX as well as uint32_t data type. However, in C++ the UINT32_MAX gets d

5条回答
  •  没有蜡笔的小新
    2021-02-02 08:00

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

    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)

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

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

提交回复
热议问题