Why is C++ numeric_limits::max() == 0?

前端 未结 4 827
忘了有多久
忘了有多久 2021-01-04 06:22

Here\'s a bit of code that might seem like it would work:

#include 
#include 

enum test { A = 1 };

int main()
{
    int max =          


        
4条回答
  •  轮回少年
    2021-01-04 07:06

    From the C++11 draft:

    In 18.3.2.1, about numeric_limits:

    Non-arithmetic standard types, such as complex (26.4.2), shall not have specializations.

    And an enum is not an arithmetic standard type.

    Then, in the non-specialized template:

    template class numeric_limits {
        public:
        [...]
        static constexpr bool is_specialized = false;
        static constexpr T max() noexcept { return T(); }
    };
    

    That is, the non-specialized max() function returns the default initialized value for that type, that is 0.

提交回复
热议问题