是否可以创建更好的`std :: min`&版本? `std :: max`? [英] Is it possible to create a better version of `std::min` & `std::max`?

查看:55
本文介绍了是否可以创建更好的`std :: min`&版本? `std :: max`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助C ++ 17的新功能,可以创建更好的 std :: min std :: max

With the new features of C++17, is it possible to create a better std::min and std::max?

我的意思是更好:

  • 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:


  • 避免了悬而未决的参考问题(例如, min(a,4); 正确运行)

  • 可以使用不同的类型(例如, min((short)4,(int)8); 编译)

  • 避免不必要的对象复制(例如,如果我有一个表示大整数的类,则仅复制它

  • 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)

是否有可能这样做,或者是 std :: min / max 当前可以使用的最佳解决方案?

Is it possible to do this, or is std::min/max the current best solution which one can have?

推荐答案

这是一个古老而失败的提议,希望得到更好的 min / max 仅使用C ++ 11技术:

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

这篇关于是否可以创建更好的`std :: min`&amp;版本? `std :: max`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆