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:
[ Parent
| on HN ]
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.
.. and Herb Sutter recommends those free functions over the member functions for this reason -- genericity.
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