Using numeric_limits::max() in constant expressions

前端 未结 5 844
無奈伤痛
無奈伤痛 2020-12-10 11:04

I would like to define inside a class a constant which value is the maximum possible int. Something like this:

class A
{
    ...
    static const int ERROR_V         


        
5条回答
  •  一生所求
    2020-12-10 11:11

    It doesn't contradict, because max is not defined static const. It's just a static member function. Functions can't be const, and static member functions can't have a const attached at the very right either.

    There is also a double max() in the double version of the limits, and in C++03 it wouldn't work to say static double const max = .... So to be consistent, max() is a function for all versions of the limit template.

    Now, it's known that max() not being able to be used like that is bad, and C++0x already solves it by making it a constexpr function, allowing your proposed usage.

提交回复
热议问题