std::less<void> and pointer types

元气小坏坏 提交于 2020-01-14 07:17:35

问题


std::less<T *> is guaranteed to provide total order, regardless of whether both pointers point into the same array.

In the latest draft of the standard, is the same true for the transparent function object std::less<void> (std::less<>) when you call its operator()?

Obviously, the same question applies to std::greater, but I assume they are specified the same.


回答1:


The current draft from github does not contain any language to that effect; in fact, its definition of less<> says explicitly "returns std::forward<T>(t) < std::forward<U>(u)", which would be undefined behaviour for incomparable pointers. So... don't do it, I suppose.

If you need a heterogeneous pointer comparator, it's probably best to write your own template predicate which uses std::less<T*>() at the appropriate moment.




回答2:


std::less<void> is undefined behavior. std::less requires either a pointer type (for which there is a special rule), or a type for which < is defined.



来源:https://stackoverflow.com/questions/21079958/stdlessvoid-and-pointer-types

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