News for hackers. Stuff that matters.
Re:My Delorean runs Perl by throwaway080383 on Saturday January 27, 2018 @11:56AM (#16246887)
Yep, in C++ you can pass in a lambda to create the comparator inline:

  std::sort(
    myvec.begin(), 
    myvec.end(), 
    [](obj& A, obj& b) {
      return A.attr < B.attr;
    }
  );
Re:My Delorean runs Perl by nrdvana on Monday January 29, 2018 @08:08PM (#16262102)
What I'm talking about is arbitrary attributes that might not exist on the base class, resulting in something like:

  std::sort(
    myvec.begin(), 
    myvec.end(), 
    [](obj& A, obj& b) {
      int A_attr1= dynamic_cast<Thing1*>(&A)? dynamic_cast<Thing1*>(&A)->attr1
                 : dynamic_cast<Thing2*>(&A)? dynamic_cast<Thing2*>(&A)->attr1
                 : -1;
      int B_attr1= dynamic_cast<Thing1*>(&B)? dynamic_cast<Thing1*>(&B)->attr1
                 : dynamic_cast<Thing2*>(&B)? dynamic_cast<Thing2*>(&B)->attr1
                 : -1;
      return A_attr1 < B_attr1;
    }
  );
and then compare that to

  @list= sort { ($a->{attr1} // -1) <=> ($b->{attr1} // -1) } @list;
Re:My Delorean runs Perl by electricslpnsld on Saturday January 27, 2018 @04:48PM (#16248261)
If you use std::begin and std::end instead of the member functions this will even work with old-school C-style arrays.
Re:My Delorean runs Perl by emmelaich on Saturday January 27, 2018 @07:36PM (#16248971)
.. and Herb Sutter recommends those free functions over the member functions for this reason -- genericity.
Re:My Delorean runs Perl by emmelaich on Saturday January 27, 2018 @07:35PM (#16248967)
Yes, and you can even use the range syntax instead of begin/end.

Here's a file with a few different ways

https://gist.github.com/anonymous/78b4f257e60d55537b36e2e4f3...

Re:My Delorean runs Perl by brianwawok on Saturday January 27, 2018 @03:54PM (#16248040)
That is disgusting as a non-C++ programmer
Re:My Delorean runs Perl by mhh__ on Saturday January 27, 2018 @04:55PM (#16248300)
eh, it's not Haskell (or even D for that matter) but C++ has come a long way in the last few years.