r203293 - [C++11] Revert uses of lambdas with array_pod_sort.

Arthur O'Dwyer arthur.j.odwyer at gmail.com
Mon Mar 10 12:12:54 PDT 2014


On Sun, Mar 9, 2014 at 9:32 PM, David Blaikie <dblaikie at gmail.com> wrote:
> On Mar 9, 2014 6:57 PM, "Richard Smith" <richard at metafoo.co.uk> wrote:
>> On Sun, Mar 9, 2014 at 6:37 PM, Arthur O'Dwyer <arthur.j.odwyer at gmail.com> wrote:
>>> >>
>>> >>> - The spaceship operator <=> is equivalent to (a < b) ? -1 : (a> b)
[…]
>> if (a.x != b.x) return a.x < b.x ? -1 : 1;
>> if (a.y != b.y) return a.y < b.y ? -1 : 1;
>> // ...
>> return 0;
[…]
> If you're suggesting that tie+op< isn't an idiom we should generally adopt
> it'd be nice to build something like it that would be appropriate. (Not sure
> if there's something that should be standardized in some form as this idiom
> seems like it will be common)

Richard's point is valid.
For the record, the efficient-but-embarrassingly-ugly "std::spaceship"
I was proposing would end up looking something like this.
[UNTESTED CODE]

namespace llvm {

template <typename... Args, std::size_t... Index>
auto subtuple_(const std::tuple<Args...>& a,
std::index_sequence<Index...>) ->
decltype(std::tie(std::get<Index+1>(a)...)) {
  return std::tie(std::get<Index+1>(a)...);
}

template<typename First, typename... Args>
auto tuple_tail(const std::tuple<First, Args...>& a) -> std::tuple<Args...>
{
    return llvm::subtuple_(a, std::make_integer_sequence<sizeof... Args - 1>());
}

template<typename T> int spaceship(const T& a, const T& b)
{
    return (a < b) ? -1 : (a > b);
}

int spaceship(const std::tuple<>& a, const std::tuple<>& b)
{
    return 0;
}

template<typename... Args>
int spaceship(const std::tuple<Args…>& a, const std::tuple<Args…>& b)
{
    int result = spaceship(std::get<0>(a), std::get<0>(b));  // uses
argument-dependent lookup
    if (result != 0) return result;
    return spaceship(tuple_tail(a), tuple_tail(b));
}

}  // namespace llvm

–Arthur




More information about the cfe-commits mailing list