r203293 - [C++11] Revert uses of lambdas with array_pod_sort.
Richard Smith
richard at metafoo.co.uk
Fri Mar 7 14:15:32 PST 2014
On Fri, Mar 7, 2014 at 2:10 PM, Arthur O'Dwyer <arthur.j.odwyer at gmail.com>wrote:
> 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
>
I bet the opposite. MSVC (non-conformingly) gives lambdas three conversions
to function pointers with different calling conventions, and this won't
compile there.
> - The spaceship operator <=> is equivalent to (a < b) ? -1 : (a > b)
>
The more usual idiom of
if (!=)
return <
naturally extends to lexicographical comparisons of multiple things. I
don't see how to do that with (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
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140307/c79dc550/attachment.html>
More information about the cfe-commits
mailing list