Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

c++ - INT_[MIN|MAX] limit macros vs numeric_limits<T>

Is there any argument for using the numeric limits macros (e.g. INT64_MAX) over std::numeric_limits<T>? From what I understand numeric_limits is in the standard but the macros are only in C99 so therefore non-standard.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The other answers mostly have correct information, but it seems that this needs updating for C++11.

In C++11, std::numeric_limits<T>::min(), std::numeric_limits<T>::max(), and std::numeric_limits<T>::lowest() are all declared constexpr, so they can be usable in most of the same contexts as INT_MIN and company. The only exception I can think of is compile-time string processing using the # stringification token.

This means that numeric_limits can be used for case labels, template parameters, etc., and you get the benefit of using it in generic code (try using INT_MIN vs. LONG_MIN in template<typename T> get_min(T t);).

C++11 also brings a solution to the issue James Kanze talks about, by adding std::numeric_limits<T>::lowest(), which gives the lowest finite value for all types, rather than the lowest value for integer types and the lowest positive value for floating-point types.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...