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

Arthur O'Dwyer arthur.j.odwyer at gmail.com
Fri Mar 7 14:10:16 PST 2014


On Fri, Mar 7, 2014 at 1:51 PM, Benjamin Kramer
<benny.kra at googlemail.com> wrote:
>
> [C++11] Revert uses of lambdas with array_pod_sort.
>
> Looks like GCC implements the lambda->function pointer conversion differently.

> -    llvm::array_pod_sort(DeferredLocs.begin(), DeferredLocs.end(),
> -                         [](const DeferredLocsTy::value_type *p1,
> -                            const DeferredLocsTy::value_type *p2) {
> -      if (p1->second->getLocStart() != p2->second->getLocStart())
> -        return p1->second->getLocStart() < p2->second->getLocStart() ? -1 : 1;
> -      return 0;
> -    });
> +    llvm::array_pod_sort(DeferredLocs.begin(), DeferredLocs.end(), SrcCmp);

Incidentally, I like the bag-of-idioms

    auto *byLocation =
        +[](const DeferredLocsTy::value_type *p1, const
DeferredLocsTy::value_type *p2) -> int {
            return (p1->second->getLocStart() <
p2->second->getLocStart() ? -1 : (p1->second->getLocStart() >
p2->second->getLocStart());
        };
    llvm::array_pod_sort(DeferredLocs.begin(), DeferredLocs.end(), byLocation);

That is,
- Name the out-of-line lambda after the predicate it implements (in
this case sorting "byLocation")
- I bet you'll find compilers are happier with the explicitly
function-pointer'ized "+[]" than with "[]" alone
- The spaceship operator <=> is equivalent to (a < b) ? -1 : (a > b)

However, FWIW and IMHO, I don't think lambdas are obviously desirable
here. This looks a lot like "C++11isms for C++11isms' sake", not
anything that would actually improve compiler speed or stability.

my two cents,
–Arthur




More information about the cfe-commits mailing list