问题
With the new features of C++17, is it possible to create a better std::min
and std::max
?
What I mean by better:
std::min/max
has the problem of dangling references.std::min/max
doesn't work with different types (i.e.,min(short, int)
needs to explicitly specify the typemin<int>(...)
)
I'd like to have a better implementation, which:
- avoids the dangling reference problem (for example,
min(a, 4);
works correctly) - works with different types (for example,
min((short)4, (int)8);
compiles) - avoids unnecessary object copies (for example, if I have a class which represents a big integer, it only copies it when it is unavoidable)
Is it possible to do this, or is std::min/max
the current best solution which one can have?
回答1:
Here's an old and failed proposal for a better min
/max
using just C++11 tech:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2199.html
来源:https://stackoverflow.com/questions/47397997/is-it-possible-to-create-a-better-version-of-stdmin-stdmax