C defines various rules regarding integer promotion, here's a great answer describing how this works.
In GCC you can do this:
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
Which will evaluate to the maximum value of the type determined by the C rules as above.
In using std::min
or std::max
using mixed types, it's necessary to provide the appropriate type to cast to as a template parameter:
size_t const subcount = std::min<Offset>(count, treecap -开发者_Go百科 offset);
To get a min/max that evaluates to the type as per the C integer promotion rules, it would be necessary to know how the types relate, and what the resultant type would be, for every combination of left and right input types.
I wish to perform a min/max, with the same behaviour as the C sample under GCC given above, and with the same promotion (thereby ensuring the same behaviour as in GCC). How can I do this in C++ and/or MSVC?
Note that immediately after performing this "natural" min/max, I'll be numeric_cast
ing to the expected size range.
There's a very good implementation here that I've used for various projects: http://www.oonumerics.org/blitz/traits.html
精彩评论