From 001080b37522017ef4401ad8b49b52102fd0f51f Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Wed, 23 Apr 2014 22:44:11 +0000 Subject: [PATCH] Use std::less instead of < in array_pod_sort's default comparator. This makes array_pod_sort portably safe to use with pointers. llvm-svn: 207043 --- llvm/include/llvm/ADT/STLExtras.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index fc45a6645ef0..7c2a147bc885 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -171,13 +171,14 @@ LLVM_CONSTEXPR inline size_t array_lengthof(T (&)[N]) { return N; } -/// array_pod_sort_comparator - This is helper function for array_pod_sort, -/// which just uses operator< on T. +/// Adapt std::less for array_pod_sort. template inline int array_pod_sort_comparator(const void *P1, const void *P2) { - if (*reinterpret_cast(P1) < *reinterpret_cast(P2)) + if (std::less()(*reinterpret_cast(P1), + *reinterpret_cast(P2))) return -1; - if (*reinterpret_cast(P2) < *reinterpret_cast(P1)) + if (std::less()(*reinterpret_cast(P2), + *reinterpret_cast(P1))) return 1; return 0; } @@ -200,7 +201,7 @@ inline int (*get_array_pod_sort_comparator(const T &)) /// possible. /// /// This function assumes that you have simple POD-like types that can be -/// compared with operator< and can be moved with memcpy. If this isn't true, +/// compared with std::less and can be moved with memcpy. If this isn't true, /// you should use std::sort. /// /// NOTE: If qsort_r were portable, we could allow a custom comparator and