C++ Logo

SG12

Advanced search

Subject: Re: [ub] Justification for < not being a total order on pointers?
From: Daniel Krügler (daniel.kruegler_at_[hidden])
Date: 2013-08-26 14:01:32


2013/8/26 Ville Voutilainen <ville.voutilainen_at_[hidden]>:
> On 26 August 2013 19:00, Jeffrey Yasskin <jyasskin_at_[hidden]> wrote:
>>
>> The library's running into problems because < is unspecified on
>> pointers that don't point to the same object. (C++14CD[expr.rel]p4)
>> Naively, this restriction makes a bit of sense, since we can't give a
>> particular answer for any two pointers, but it's causing problems for
>> some generic library components. Specifically:
>>
>> std::map<T*> works because std::less has a special case for pointers.
>> ([comparisons]p14)
>>
>> std::map<std::tuple<T*>> gives undefined behavior because
>> std::less<tuple> uses tuple's operator<, which uses T*'s operator<.
>>
>> std::map<std::optional<T*>> goes back to defined behavior because we
>> special-cased optional's operator< to use std::less:
>> [optional.relops]p4
>
> I have strong reasons to believe we'll see an NB comment proposing std::less
> to be specialized for tuple<T*> and containers.

I don't understand why there should be different specializations
instead of a single specialization:

less<tuple<T...>>

which defines the operator() overload in terms of std::less<T> for
each T in order. Same thing for pair and optional and containers.

- Daniel


SG12 list run by herb.sutter at gmail.com