2013/2/15 Tony V E <
tvan...@gmail.com>:
> IIUC, operator>() for containers and tuple, is defined via operator<()
> and NOT defined via operator>() of the underlying value type. ie
>
> #include <tuple>
>
> struct MyType
> {
> bool operator<(MyType const &) const { return false; }
> bool operator>(MyType const &) const { return true; }
> };
>
> int main()
> {
> std::tuple<MyType> t, u;
>
> return t > u;
> }
>
> returns 0, not 1.
>
> Is this expected? Or a defect?
I don't see any reason for a defect. In fact, the library explicitly
gives wording that makes this choices intentional, see [operators]
p10:
"In this library, whenever a declaration is provided for an
operator!=, operator>, operator>=, or operator<=,
and requirements and semantics are not explicitly provided, the
requirements and semantics are as specified
in this Clause."
Basically all newer class templates have decided to make this
explicit, only some "older" ones (like std::reverse_iterator) don't.
> If expected, would you apply this to the proposed std::optional<>?
Yes.
> int main()
> {
> std::optional<MyType> t, u;
>
> return t > u;
> }
>
> I would expect optional<MyType> to work as much like MyType as
> possible, but that doesn't follow the pattern of containers and tuple.
A similar example is the std::chrono::duration template, whose
comparison functions are solely defined in terms of == and <. This
harmonizes with [operators] p10 and should be the recommended way for
future types, IMO.
> Not that containers or tuple are the same as optional, but there is
> similarity.
I think the similarity is quite strong.
- Daniel