Is it possible to create a better version of `std::min` & `std::max`?

蹲街弑〆低调 提交于 2019-12-08 12:43:40

问题


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 type min<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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!