Problem with numeric_limits

Why this doesn't work?

enum : long {MaxValue = std::numeric_limits<long int>::max()};

I'm getting error :Error 1 error C2057: expected constant expression
What isn't constant about it? Limits of long int are known at compile time so what's the problem?

Upvotes: 5

Views: 1470

Answers (3)

Daniel Lidstr&#246;m
Daniel Lidstr&#246;m

Reputation: 10280

As the other have said, you need a constant expression (functions don't qualify). Eventually C++1x will support a wider range of constant expressions, including functions. See n2235 and Bjarne Stroustrup's FAQ entry.

Upvotes: 3

Aamir
Aamir

Reputation: 15566

Yes, but a function cannot be executed at compile time. max()

Upvotes: 1

Vladimir
Vladimir

Reputation: 170849

The problem is that although std::numeric_limits<long int>::max() function returns constant value it is called in run-time and you need constant value in compile-time

Probably you can just use LONG_MAX value instead (see climits header)?

Upvotes: 5

Related Questions