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

前端 未结 4 801
忘了有多久
忘了有多久 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:03

    The numeric_limits is a regular class template, it is not connected to the compiler in any special way as to find out about user-defined enum types. If you look at the file, it has the default template definition that returns zeros for everything, and a whole bunch of type-specific specifications for the individual types, returning the right constants.

    You can "plug in" your enum into numeric_limits by providing a specification of numeric_limits by yourself. You can copy the one for int from the , and modify it to suit your needs.

提交回复
热议问题