Topic: order_by
Author: strasser.ben@gmail.com
Date: Fri, 30 May 2014 03:45:05 -0700 (PDT)
Raw View
------=_Part_65_13612801.1401446705489
Content-Type: text/plain; charset=UTF-8
Hi,
(if this is a duplicate then please excuse the noise :) )
I find myself commonly writing code of the following form:
struct Point{
int x,y;
};
vector<Point>vec;
sort(vec.begin(), vec.end(), [](Point l, Point r){return l.x < r.x;});
The code sorts an array of structs by one of the members. It does not
matter in which order the y members come. To reduce the boilerplate code
(and thus a source of hard to find typo bugs like swapping an x for a y) I
use the following utility:
template<class F>
struct OrderBy{
const F&f;
template<class T>
bool operator()(const T&l, const T&r)const{
return f(l) < f(r);
}
};
template<class F>
OrderBy<F>order_by(const F&f){
return {f};
}
This can be used as following:
struct Point{
int x,y;
};
vector<Point>vec;
sort(vec.begin(), vec.end(), order_by([](Point p){return p.x;}));
Up to now order_by is only a neat addition but nothing ground breaking. It
really shines in combination with tuple:
struct S{
int x,y,z,p,q,r;
};
vector<S>vec;
sort(vec.begin(), vec.end(), order_by([](S p){return make_tuple(p.x, p.p, p.
q, p.y);}));
The meaning here is: First sort by x, then by p, then by q and finally by
y. The rest are can come in any order. Without order_by you would need
something like:
[](S l, S r){
if(l.x != r.x)
return l.x < r.x;
if(l.p != r.p)
return l.p < r.p;
if(l.q != r.q)
return l.q < r.q;
if(l.y != r.y)
return l.y < r.y;
return false;
}
Best Regards,
Ben Strasser
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_65_13612801.1401446705489
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hi,<br><br>(if this is a duplicate then please excuse the =
noise :) )<br><br>I find myself commonly writing code of the following form=
:<br><br><div class=3D"prettyprint" style=3D"background-color: rgb(250, 250=
, 250); border-color: rgb(187, 187, 187); border-style: solid; border-width=
: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"su=
bprettyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">str=
uct</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span style=3D"color: #606;" class=3D"styled-by-prettify">Point</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br> </span><span styl=
e=3D"color: #008;" class=3D"styled-by-prettify">int</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> x</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">y</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
};</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br>=
vector</span><span style=3D"color: #660;" class=3D"styled-by-prettify"><=
</span><span style=3D"color: #606;" class=3D"styled-by-prettify">Point</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">></span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify">vec</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br>sort</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify">vec</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">.</span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">begin</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">(),</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
vec</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</span=
><span style=3D"color: #008;" class=3D"styled-by-prettify">end</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">(),</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">[](</span><span style=3D"color: #606;=
" class=3D"styled-by-prettify">Point</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> l</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">=
Point</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> r</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">){</span><spa=
n style=3D"color: #008;" class=3D"styled-by-prettify">return</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> l</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify">x </span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify"><</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> r</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">x</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;});</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span></=
div></code></div><br><br><br>The code sorts an array of structs by one of t=
he members. It does not matter in which order the y members come. To reduce=
the boilerplate code (and thus a source of hard to find typo bugs like swa=
pping an x for a y) I use the following utility:<br><br><div class=3D"prett=
yprint" style=3D"background-color: rgb(250, 250, 250); border-color: rgb(18=
7, 187, 187); border-style: solid; border-width: 1px; word-wrap: break-word=
;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D=
"color: #008;" class=3D"styled-by-prettify">template</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify"><</span><span style=3D"color: =
#008;" class=3D"styled-by-prettify">class</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> F</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">></span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"><br></span><span style=3D"color: #008;" class=3D"styled-b=
y-prettify">struct</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">O=
rderBy</span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"><br> </=
span><span style=3D"color: #008;" class=3D"styled-by-prettify">const</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> F</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">&</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">f</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"><br> </span><span style=3D"color: #008;"=
class=3D"styled-by-prettify">template</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify"><</span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">class</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> T</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">></span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"><br> </span><span style=3D"color: #008;" class=3D"styled-by-pret=
tify">bool</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
</span><span style=3D"color: #008;" class=3D"styled-by-prettify">operator<=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">()(</span><=
span style=3D"color: #008;" class=3D"styled-by-prettify">const</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> T</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">&</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">l</span><span style=3D"color: #660;"=
class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-b=
y-prettify">const</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> T</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&=
amp;</span><span style=3D"color: #000;" class=3D"styled-by-prettify">r</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">const</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br> </span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">return</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> f</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify">l</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">)</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">&=
lt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> f</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify">r</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">);</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"><br> </span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">};</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"><br></span><span style=3D"color: #008;" class=3D"styled-by-pret=
tify">template</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy"><</span><span style=3D"color: #008;" class=3D"styled-by-prettify">cl=
ass</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> F</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">></span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span styl=
e=3D"color: #606;" class=3D"styled-by-prettify">OrderBy</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify"><</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify">F</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">></span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify">order_by</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">(</span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">const</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> F</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">&</span><span style=3D"color: #000;" class=3D"styled-by-prettify">f</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">){</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br> </span><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">return</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify">f</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">};</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"><br></span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"><br><br></span></div></code></div><br><br><br>This can be used as followi=
ng:<br><br><div class=3D"prettyprint" style=3D"background-color: rgb(250, 2=
50, 250); border-color: rgb(187, 187, 187); border-style: solid; border-wid=
th: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"=
subprettyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">s=
truct</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span style=3D"color: #606;" class=3D"styled-by-prettify">Point</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br> </span><span styl=
e=3D"color: #008;" class=3D"styled-by-prettify">int</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> x</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">y</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
};</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br>=
vector</span><span style=3D"color: #660;" class=3D"styled-by-prettify"><=
</span><span style=3D"color: #606;" class=3D"styled-by-prettify">Point</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">></span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify">vec</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br>sort</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify">vec</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">.</span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">begin</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">(),</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
vec</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</span=
><span style=3D"color: #008;" class=3D"styled-by-prettify">end</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">(),</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> order_by</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">([](</span><span style=3D"co=
lor: #606;" class=3D"styled-by-prettify">Point</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> p</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">){</span><span style=3D"color: #008;" class=3D"=
styled-by-prettify">return</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> p</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">x=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;}));</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span></di=
v></code></div><br><br><br>Up to now order_by is only a neat addition but n=
othing ground breaking. It really shines in combination with tuple:<br><br>=
<div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); b=
order-color: rgb(187, 187, 187); border-style: solid; border-width: 1px; wo=
rd-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subprettypr=
int"><span style=3D"color: #008;" class=3D"styled-by-prettify">struct</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> S</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"><br> </span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">int</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> x</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify">y</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
>z</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify">p</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify">q</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">r</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
};</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br>=
vector</span><span style=3D"color: #660;" class=3D"styled-by-prettify"><=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify">S</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">></span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify">vec</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"><br>sort</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">vec</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">.</span><span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">begin</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">(),</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> vec=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</span><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">end</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">(),</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> order_by</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">([](</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify">S p</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">){</span><span style=3D"color: #008;" class=3D"=
styled-by-prettify">return</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> make_tuple</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify">p</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify">x</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> p</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify">p</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> p</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">q</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> p</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify">y</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">);}));</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span></div></code></div><br><br><br>The meani=
ng here is: First sort by x, then by p, then by q and finally by y. The res=
t are can come in any order. Without order_by you would need something like=
:<br><br><div class=3D"prettyprint" style=3D"background-color: rgb(250, 250=
, 250); border-color: rgb(187, 187, 187); border-style: solid; border-width=
: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"su=
bprettyprint"><span style=3D"color: #660;" class=3D"styled-by-prettify">[](=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify">S l</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> S r</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">){</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"><br> </span><span style=3D"color: =
#008;" class=3D"styled-by-prettify">if</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify">l</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
>x </span><span style=3D"color: #660;" class=3D"styled-by-prettify">!=3D</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> r</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">x</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"><br> </span><span style=3D"color:=
#008;" class=3D"styled-by-prettify">return</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> l</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">x </span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify"><</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> r</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify">x</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"><br> </span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">if</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify">l</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">.</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify">p </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
!=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> r</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">.</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify">p</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">)</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"><br> </span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">return</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> l</span><span style=3D"color: #660;"=
class=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify">p </span><span style=3D"color: #660;" class=3D"styled-=
by-prettify"><</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> r</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify">p</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br> </span><span styl=
e=3D"color: #008;" class=3D"styled-by-prettify">if</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify">l</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">q </span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">!=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> r</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify">q</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">)</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br> </span><span sty=
le=3D"color: #008;" class=3D"styled-by-prettify">return</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> l</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify">q </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify"><</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> r</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
>q</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"><br> </span=
><span style=3D"color: #008;" class=3D"styled-by-prettify">if</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify">l</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">y </span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">!=3D</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> r</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">y</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"><br> </span>=
<span style=3D"color: #008;" class=3D"styled-by-prettify">return</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> l</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">y </span><span style=3D"color: #660;"=
class=3D"styled-by-prettify"><</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> r</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">.</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify">y</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br> =
</span><span style=3D"color: #008;" class=3D"styled-by-prettify">return</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span =
style=3D"color: #008;" class=3D"styled-by-prettify">false</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span></div></code></div><br><br>Best Regards,=
<br>Ben Strasser<br><br></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_65_13612801.1401446705489--
.
Author: Andrew Tomazos <andrewtomazos@gmail.com>
Date: Fri, 30 May 2014 13:30:01 +0200
Raw View
--20cf303bf5e0c3684904fa9c5db6
Content-Type: text/plain; charset=UTF-8
A couple of things to look at would be the built-in pointer-to-member type:
sort_by_member(vec.begin(), vec.end(), &Point::x);
or
sort(vec.begin(), vec.end(), order_by(&Point::x));
and also using std::tie instead of std::make_tuple to avoid copying:
sort(vec.begin(), vec.end(), order_by([](const S& p){return tie(p.x,p.p, p.q
, p.y);}));
On Fri, May 30, 2014 at 12:45 PM, <strasser.ben@gmail.com> wrote:
> Hi,
>
> (if this is a duplicate then please excuse the noise :) )
>
> I find myself commonly writing code of the following form:
>
> struct Point{
> int x,y;
> };
>
> vector<Point>vec;
> sort(vec.begin(), vec.end(), [](Point l, Point r){return l.x < r.x;});
>
>
>
> The code sorts an array of structs by one of the members. It does not
> matter in which order the y members come. To reduce the boilerplate code
> (and thus a source of hard to find typo bugs like swapping an x for a y) I
> use the following utility:
>
> template<class F>
> struct OrderBy{
> const F&f;
> template<class T>
> bool operator()(const T&l, const T&r)const{
> return f(l) < f(r);
> }
> };
> template<class F>
> OrderBy<F>order_by(const F&f){
> return {f};
> }
>
>
>
>
> This can be used as following:
>
> struct Point{
> int x,y;
> };
>
> vector<Point>vec;
> sort(vec.begin(), vec.end(), order_by([](Point p){return p.x;}));
>
>
>
> Up to now order_by is only a neat addition but nothing ground breaking. It
> really shines in combination with tuple:
>
> struct S{
> int x,y,z,p,q,r;
> };
>
> vector<S>vec;
> sort(vec.begin(), vec.end(), order_by([](S p){return make_tuple(p.x, p.p,
> p.q, p.y);}));
>
>
>
> The meaning here is: First sort by x, then by p, then by q and finally by
> y. The rest are can come in any order. Without order_by you would need
> something like:
>
> [](S l, S r){
> if(l.x != r.x)
> return l.x < r.x;
> if(l.p != r.p)
> return l.p < r.p;
> if(l.q != r.q)
> return l.q < r.q;
> if(l.y != r.y)
> return l.y < r.y;
> return false;
> }
>
>
> Best Regards,
> Ben Strasser
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> Visit this group at
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--20cf303bf5e0c3684904fa9c5db6
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">A couple of things to look at would be the built-in pointe=
r-to-member type:<div><br></div><div><span style=3D"font-family:monospace;f=
ont-size:10.5px;background-color:rgb(250,250,250)"><font color=3D"#000000">=
sort_by_member</font></span><span style=3D"font-family:monospace;font-size:=
10.5px;background-color:rgb(250,250,250);color:rgb(102,102,0)">(</span><spa=
n style=3D"font-family:monospace;font-size:10.5px;background-color:rgb(250,=
250,250);color:rgb(0,0,0)">vec</span><span style=3D"font-family:monospace;f=
ont-size:10.5px;background-color:rgb(250,250,250);color:rgb(102,102,0)">.</=
span><span style=3D"font-family:monospace;font-size:10.5px;background-color=
:rgb(250,250,250);color:rgb(0,0,136)">begin</span><span style=3D"font-famil=
y:monospace;font-size:10.5px;background-color:rgb(250,250,250);color:rgb(10=
2,102,0)">(),</span><span style=3D"font-family:monospace;font-size:10.5px;b=
ackground-color:rgb(250,250,250);color:rgb(0,0,0)">=C2=A0vec</span><span st=
yle=3D"font-family:monospace;font-size:10.5px;background-color:rgb(250,250,=
250);color:rgb(102,102,0)">.</span><span style=3D"font-family:monospace;fon=
t-size:10.5px;background-color:rgb(250,250,250);color:rgb(0,0,136)">end</sp=
an><span style=3D"font-family:monospace;font-size:10.5px;background-color:r=
gb(250,250,250);color:rgb(102,102,0)">(),</span><span style=3D"font-family:=
monospace;font-size:10.5px;background-color:rgb(250,250,250);color:rgb(0,0,=
0)">=C2=A0&Point::x);</span><br>
</div><div><br></div><div>or</div><div><br></div><div><div><span style=3D"f=
ont-family:monospace;font-size:10.5px;background-color:rgb(250,250,250)"><f=
ont color=3D"#000000">sort</font></span><span style=3D"font-family:monospac=
e;font-size:10.5px;background-color:rgb(250,250,250);color:rgb(102,102,0)">=
(</span><span style=3D"font-family:monospace;font-size:10.5px;background-co=
lor:rgb(250,250,250);color:rgb(0,0,0)">vec</span><span style=3D"font-family=
:monospace;font-size:10.5px;background-color:rgb(250,250,250);color:rgb(102=
,102,0)">.</span><span style=3D"font-family:monospace;font-size:10.5px;back=
ground-color:rgb(250,250,250);color:rgb(0,0,136)">begin</span><span style=
=3D"font-family:monospace;font-size:10.5px;background-color:rgb(250,250,250=
);color:rgb(102,102,0)">(),</span><span style=3D"font-family:monospace;font=
-size:10.5px;background-color:rgb(250,250,250);color:rgb(0,0,0)">=C2=A0vec<=
/span><span style=3D"font-family:monospace;font-size:10.5px;background-colo=
r:rgb(250,250,250);color:rgb(102,102,0)">.</span><span style=3D"font-family=
:monospace;font-size:10.5px;background-color:rgb(250,250,250);color:rgb(0,0=
,136)">end</span><span style=3D"font-family:monospace;font-size:10.5px;back=
ground-color:rgb(250,250,250);color:rgb(102,102,0)">(),</span><span style=
=3D"font-family:monospace;font-size:10.5px;background-color:rgb(250,250,250=
);color:rgb(0,0,0)">=C2=A0order_by(&Point::x));</span><br>
</div></div><div><span style=3D"font-family:monospace;font-size:10.5px;back=
ground-color:rgb(250,250,250);color:rgb(0,0,0)"><br></span></div><div><span=
style=3D"font-family:monospace;font-size:10.5px;background-color:rgb(250,2=
50,250);color:rgb(0,0,0)">and also using std::tie instead of std::make_tupl=
e to avoid copying:</span></div>
<div><span style=3D"font-family:monospace;font-size:10.5px;background-color=
:rgb(250,250,250);color:rgb(0,0,0)"><br></span></div><div><span style=3D"fo=
nt-family:monospace;font-size:10.5px;background-color:rgb(250,250,250);colo=
r:rgb(0,0,0)">sort</span><span style=3D"font-family:monospace;font-size:10.=
5px;background-color:rgb(250,250,250);color:rgb(102,102,0)">(</span><span s=
tyle=3D"font-family:monospace;font-size:10.5px;background-color:rgb(250,250=
,250);color:rgb(0,0,0)">vec</span><span style=3D"font-family:monospace;font=
-size:10.5px;background-color:rgb(250,250,250);color:rgb(102,102,0)">.</spa=
n><span style=3D"font-family:monospace;font-size:10.5px;background-color:rg=
b(250,250,250);color:rgb(0,0,136)">begin</span><span style=3D"font-family:m=
onospace;font-size:10.5px;background-color:rgb(250,250,250);color:rgb(102,1=
02,0)">(),</span><span style=3D"font-family:monospace;font-size:10.5px;back=
ground-color:rgb(250,250,250);color:rgb(0,0,0)">=C2=A0vec</span><span style=
=3D"font-family:monospace;font-size:10.5px;background-color:rgb(250,250,250=
);color:rgb(102,102,0)">.</span><span style=3D"font-family:monospace;font-s=
ize:10.5px;background-color:rgb(250,250,250);color:rgb(0,0,136)">end</span>=
<span style=3D"font-family:monospace;font-size:10.5px;background-color:rgb(=
250,250,250);color:rgb(102,102,0)">(),</span><span style=3D"font-family:mon=
ospace;font-size:10.5px;background-color:rgb(250,250,250);color:rgb(0,0,0)"=
>=C2=A0order_by</span><span style=3D"font-family:monospace;font-size:10.5px=
;background-color:rgb(250,250,250);color:rgb(102,102,0)">([](const=C2=A0</s=
pan><span style=3D"font-family:monospace;font-size:10.5px;background-color:=
rgb(250,250,250);color:rgb(0,0,0)">S& p</span><span style=3D"font-famil=
y:monospace;font-size:10.5px;background-color:rgb(250,250,250);color:rgb(10=
2,102,0)">){</span><span style=3D"font-family:monospace;font-size:10.5px;ba=
ckground-color:rgb(250,250,250);color:rgb(0,0,136)">return</span><span styl=
e=3D"font-family:monospace;font-size:10.5px;background-color:rgb(250,250,25=
0);color:rgb(0,0,0)">=C2=A0tie</span><span style=3D"font-family:monospace;f=
ont-size:10.5px;background-color:rgb(250,250,250);color:rgb(102,102,0)">(</=
span><span style=3D"font-family:monospace;font-size:10.5px;background-color=
:rgb(250,250,250);color:rgb(0,0,0)">p</span><span style=3D"font-family:mono=
space;font-size:10.5px;background-color:rgb(250,250,250);color:rgb(102,102,=
0)">.</span><span style=3D"font-family:monospace;font-size:10.5px;backgroun=
d-color:rgb(250,250,250);color:rgb(0,0,0)">x</span><span style=3D"font-fami=
ly:monospace;font-size:10.5px;background-color:rgb(250,250,250);color:rgb(1=
02,102,0)">,</span><span style=3D"font-family:monospace;font-size:10.5px;ba=
ckground-color:rgb(250,250,250);color:rgb(0,0,0)">p</span><span style=3D"fo=
nt-family:monospace;font-size:10.5px;background-color:rgb(250,250,250);colo=
r:rgb(102,102,0)">.</span><span style=3D"font-family:monospace;font-size:10=
..5px;background-color:rgb(250,250,250);color:rgb(0,0,0)">p</span><span styl=
e=3D"font-family:monospace;font-size:10.5px;background-color:rgb(250,250,25=
0);color:rgb(102,102,0)">,</span><span style=3D"font-family:monospace;font-=
size:10.5px;background-color:rgb(250,250,250);color:rgb(0,0,0)">=C2=A0p</sp=
an><span style=3D"font-family:monospace;font-size:10.5px;background-color:r=
gb(250,250,250);color:rgb(102,102,0)">.</span><span style=3D"font-family:mo=
nospace;font-size:10.5px;background-color:rgb(250,250,250);color:rgb(0,0,0)=
">q</span><span style=3D"font-family:monospace;font-size:10.5px;background-=
color:rgb(250,250,250);color:rgb(102,102,0)">,</span><span style=3D"font-fa=
mily:monospace;font-size:10.5px;background-color:rgb(250,250,250);color:rgb=
(0,0,0)">=C2=A0p</span><span style=3D"font-family:monospace;font-size:10.5p=
x;background-color:rgb(250,250,250);color:rgb(102,102,0)">.</span><span sty=
le=3D"font-family:monospace;font-size:10.5px;background-color:rgb(250,250,2=
50);color:rgb(0,0,0)">y</span><span style=3D"font-family:monospace;font-siz=
e:10.5px;background-color:rgb(250,250,250);color:rgb(102,102,0)">);}));</sp=
an><span style=3D"font-family:monospace;font-size:10.5px;background-color:r=
gb(250,250,250);color:rgb(0,0,0)"><br>
</span></div><div><br></div></div><div class=3D"gmail_extra"><br><br><div c=
lass=3D"gmail_quote">On Fri, May 30, 2014 at 12:45 PM, <span dir=3D"ltr">&=
lt;<a href=3D"mailto:strasser.ben@gmail.com" target=3D"_blank">strasser.ben=
@gmail.com</a>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div dir=3D"ltr">Hi,<br><br>(if this is a du=
plicate then please excuse the noise :) )<br><br>I find myself commonly wri=
ting code of the following form:<br>
<br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,18=
7,187);border-style:solid;border-width:1px;word-wrap:break-word"><code><div=
><span style=3D"color:#008">struct</span><span style=3D"color:#000"> </span=
><span style=3D"color:#606">Point</span><span style=3D"color:#660">{</span>=
<span style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">int</span><span style=3D"color:#00=
0"> x</span><span style=3D"color:#660">,</span><span style=3D"color:#000">y=
</span><span style=3D"color:#660">;</span><span style=3D"color:#000"><br></=
span><span style=3D"color:#660">};</span><span style=3D"color:#000"><br>
<br>vector</span><span style=3D"color:#660"><</span><span style=3D"color=
:#606">Point</span><span style=3D"color:#660">></span><span style=3D"col=
or:#000">vec</span><span style=3D"color:#660">;</span><span style=3D"color:=
#000"><br>
sort</span><span style=3D"color:#660">(</span><span style=3D"color:#000">ve=
c</span><span style=3D"color:#660">.</span><span style=3D"color:#008">begin=
</span><span style=3D"color:#660">(),</span><span style=3D"color:#000"> vec=
</span><span style=3D"color:#660">.</span><span style=3D"color:#008">end</s=
pan><span style=3D"color:#660">(),</span><span style=3D"color:#000"> </span=
><span style=3D"color:#660">[](</span><span style=3D"color:#606">Point</spa=
n><span style=3D"color:#000"> l</span><span style=3D"color:#660">,</span><s=
pan style=3D"color:#000"> </span><span style=3D"color:#606">Point</span><sp=
an style=3D"color:#000"> r</span><span style=3D"color:#660">){</span><span =
style=3D"color:#008">return</span><span style=3D"color:#000"> l</span><span=
style=3D"color:#660">.</span><span style=3D"color:#000">x </span><span sty=
le=3D"color:#660"><</span><span style=3D"color:#000"> r</span><span styl=
e=3D"color:#660">.</span><span style=3D"color:#000">x</span><span style=3D"=
color:#660">;});</span><span style=3D"color:#000"><br>
</span></div></code></div><br><br><br>The code sorts an array of structs by=
one of the members. It does not matter in which order the y members come. =
To reduce the boilerplate code (and thus a source of hard to find typo bugs=
like swapping an x for a y) I use the following utility:<br>
<br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,18=
7,187);border-style:solid;border-width:1px;word-wrap:break-word"><code><div=
><span style=3D"color:#008">template</span><span style=3D"color:#660"><<=
/span><span style=3D"color:#008">class</span><span style=3D"color:#000"> F<=
/span><span style=3D"color:#660">></span><span style=3D"color:#000"><br>
</span><span style=3D"color:#008">struct</span><span style=3D"color:#000"> =
</span><span style=3D"color:#606">OrderBy</span><span style=3D"color:#660">=
{</span><span style=3D"color:#000"><br>=C2=A0 </span><span style=3D"color:#=
008">const</span><span style=3D"color:#000"> F</span><span style=3D"color:#=
660">&</span><span style=3D"color:#000">f</span><span style=3D"color:#6=
60">;</span><span style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">template</span><span style=3D"colo=
r:#660"><</span><span style=3D"color:#008">class</span><span style=3D"co=
lor:#000"> T</span><span style=3D"color:#660">></span><span style=3D"col=
or:#000"><br>
=C2=A0 </span><span style=3D"color:#008">bool</span><span style=3D"color:#0=
00"> </span><span style=3D"color:#008">operator</span><span style=3D"color:=
#660">()(</span><span style=3D"color:#008">const</span><span style=3D"color=
:#000"> T</span><span style=3D"color:#660">&</span><span style=3D"color=
:#000">l</span><span style=3D"color:#660">,</span><span style=3D"color:#000=
"> </span><span style=3D"color:#008">const</span><span style=3D"color:#000"=
> T</span><span style=3D"color:#660">&</span><span style=3D"color:#000"=
>r</span><span style=3D"color:#660">)</span><span style=3D"color:#008">cons=
t</span><span style=3D"color:#660">{</span><span style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span><span style=3D=
"color:#000"> f</span><span style=3D"color:#660">(</span><span style=3D"col=
or:#000">l</span><span style=3D"color:#660">)</span><span style=3D"color:#0=
00"> </span><span style=3D"color:#660"><</span><span style=3D"color:#000=
"> f</span><span style=3D"color:#660">(</span><span style=3D"color:#000">r<=
/span><span style=3D"color:#660">);</span><span style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#660">}</span><span style=3D"color:#000"=
><br></span><span style=3D"color:#660">};</span><span style=3D"color:#000">=
<br></span><span style=3D"color:#008">template</span><span style=3D"color:#=
660"><</span><span style=3D"color:#008">class</span><span style=3D"color=
:#000"> F</span><span style=3D"color:#660">></span><span style=3D"color:=
#000"><br>
</span><span style=3D"color:#606">OrderBy</span><span style=3D"color:#660">=
<</span><span style=3D"color:#000">F</span><span style=3D"color:#660">&g=
t;</span><span style=3D"color:#000">order_by</span><span style=3D"color:#66=
0">(</span><span style=3D"color:#008">const</span><span style=3D"color:#000=
"> F</span><span style=3D"color:#660">&</span><span style=3D"color:#000=
">f</span><span style=3D"color:#660">){</span><span style=3D"color:#000"><b=
r>
=C2=A0 </span><span style=3D"color:#008">return</span><span style=3D"color:=
#000"> </span><span style=3D"color:#660">{</span><span style=3D"color:#000"=
>f</span><span style=3D"color:#660">};</span><span style=3D"color:#000"><br=
></span><span style=3D"color:#660">}</span><span style=3D"color:#000"><br>
<br></span></div></code></div><br><br><br>This can be used as following:<br=
><br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,1=
87,187);border-style:solid;border-width:1px;word-wrap:break-word"><code><di=
v>
<span style=3D"color:#008">struct</span><span style=3D"color:#000"> </span>=
<span style=3D"color:#606">Point</span><span style=3D"color:#660">{</span><=
span style=3D"color:#000"><br>=C2=A0 </span><span style=3D"color:#008">int<=
/span><span style=3D"color:#000"> x</span><span style=3D"color:#660">,</spa=
n><span style=3D"color:#000">y</span><span style=3D"color:#660">;</span><sp=
an style=3D"color:#000"><br>
</span><span style=3D"color:#660">};</span><span style=3D"color:#000"><br><=
br>vector</span><span style=3D"color:#660"><</span><span style=3D"color:=
#606">Point</span><span style=3D"color:#660">></span><span style=3D"colo=
r:#000">vec</span><span style=3D"color:#660">;</span><span style=3D"color:#=
000"><br>
sort</span><span style=3D"color:#660">(</span><span style=3D"color:#000">ve=
c</span><span style=3D"color:#660">.</span><span style=3D"color:#008">begin=
</span><span style=3D"color:#660">(),</span><span style=3D"color:#000"> vec=
</span><span style=3D"color:#660">.</span><span style=3D"color:#008">end</s=
pan><span style=3D"color:#660">(),</span><span style=3D"color:#000"> order_=
by</span><span style=3D"color:#660">([](</span><span style=3D"color:#606">P=
oint</span><span style=3D"color:#000"> p</span><span style=3D"color:#660">)=
{</span><span style=3D"color:#008">return</span><span style=3D"color:#000">=
p</span><span style=3D"color:#660">.</span><span style=3D"color:#000">x</s=
pan><span style=3D"color:#660">;}));</span><span style=3D"color:#000"><br>
</span></div></code></div><br><br><br>Up to now order_by is only a neat add=
ition but nothing ground breaking. It really shines in combination with tup=
le:<br><br><div style=3D"background-color:rgb(250,250,250);border-color:rgb=
(187,187,187);border-style:solid;border-width:1px;word-wrap:break-word">
<code><div><span style=3D"color:#008">struct</span><span style=3D"color:#00=
0"> S</span><span style=3D"color:#660">{</span><span style=3D"color:#000"><=
br>=C2=A0 </span><span style=3D"color:#008">int</span><span style=3D"color:=
#000"> x</span><span style=3D"color:#660">,</span><span style=3D"color:#000=
">y</span><span style=3D"color:#660">,</span><span style=3D"color:#000">z</=
span><span style=3D"color:#660">,</span><span style=3D"color:#000">p</span>=
<span style=3D"color:#660">,</span><span style=3D"color:#000">q</span><span=
style=3D"color:#660">,</span><span style=3D"color:#000">r</span><span styl=
e=3D"color:#660">;</span><span style=3D"color:#000"><br>
</span><span style=3D"color:#660">};</span><span style=3D"color:#000"><br><=
br>vector</span><span style=3D"color:#660"><</span><span style=3D"color:=
#000">S</span><span style=3D"color:#660">></span><span style=3D"color:#0=
00">vec</span><span style=3D"color:#660">;</span><span style=3D"color:#000"=
><br>
sort</span><span style=3D"color:#660">(</span><span style=3D"color:#000">ve=
c</span><span style=3D"color:#660">.</span><span style=3D"color:#008">begin=
</span><span style=3D"color:#660">(),</span><span style=3D"color:#000"> vec=
</span><span style=3D"color:#660">.</span><span style=3D"color:#008">end</s=
pan><span style=3D"color:#660">(),</span><span style=3D"color:#000"> order_=
by</span><span style=3D"color:#660">([](</span><span style=3D"color:#000">S=
p</span><span style=3D"color:#660">){</span><span style=3D"color:#008">ret=
urn</span><span style=3D"color:#000"> make_tuple</span><span style=3D"color=
:#660">(</span><span style=3D"color:#000">p</span><span style=3D"color:#660=
">.</span><span style=3D"color:#000">x</span><span style=3D"color:#660">,</=
span><span style=3D"color:#000"> p</span><span style=3D"color:#660">.</span=
><span style=3D"color:#000">p</span><span style=3D"color:#660">,</span><spa=
n style=3D"color:#000"> p</span><span style=3D"color:#660">.</span><span st=
yle=3D"color:#000">q</span><span style=3D"color:#660">,</span><span style=
=3D"color:#000"> p</span><span style=3D"color:#660">.</span><span style=3D"=
color:#000">y</span><span style=3D"color:#660">);}));</span><span style=3D"=
color:#000"><br>
</span></div></code></div><br><br><br>The meaning here is: First sort by x,=
then by p, then by q and finally by y. The rest are can come in any order.=
Without order_by you would need something like:<br><br><div style=3D"backg=
round-color:rgb(250,250,250);border-color:rgb(187,187,187);border-style:sol=
id;border-width:1px;word-wrap:break-word">
<code><div><span style=3D"color:#660">[](</span><span style=3D"color:#000">=
S l</span><span style=3D"color:#660">,</span><span style=3D"color:#000"> S =
r</span><span style=3D"color:#660">){</span><span style=3D"color:#000"><br>=
=C2=A0 </span><span style=3D"color:#008">if</span><span style=3D"color:#660=
">(</span><span style=3D"color:#000">l</span><span style=3D"color:#660">.</=
span><span style=3D"color:#000">x </span><span style=3D"color:#660">!=3D</s=
pan><span style=3D"color:#000"> r</span><span style=3D"color:#660">.</span>=
<span style=3D"color:#000">x</span><span style=3D"color:#660">)</span><span=
style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span><span style=3D=
"color:#000"> l</span><span style=3D"color:#660">.</span><span style=3D"col=
or:#000">x </span><span style=3D"color:#660"><</span><span style=3D"colo=
r:#000"> r</span><span style=3D"color:#660">.</span><span style=3D"color:#0=
00">x</span><span style=3D"color:#660">;</span><span style=3D"color:#000"><=
br>
=C2=A0 </span><span style=3D"color:#008">if</span><span style=3D"color:#660=
">(</span><span style=3D"color:#000">l</span><span style=3D"color:#660">.</=
span><span style=3D"color:#000">p </span><span style=3D"color:#660">!=3D</s=
pan><span style=3D"color:#000"> r</span><span style=3D"color:#660">.</span>=
<span style=3D"color:#000">p</span><span style=3D"color:#660">)</span><span=
style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span><span style=3D=
"color:#000"> l</span><span style=3D"color:#660">.</span><span style=3D"col=
or:#000">p </span><span style=3D"color:#660"><</span><span style=3D"colo=
r:#000"> r</span><span style=3D"color:#660">.</span><span style=3D"color:#0=
00">p</span><span style=3D"color:#660">;</span><span style=3D"color:#000"><=
br>
=C2=A0 </span><span style=3D"color:#008">if</span><span style=3D"color:#660=
">(</span><span style=3D"color:#000">l</span><span style=3D"color:#660">.</=
span><span style=3D"color:#000">q </span><span style=3D"color:#660">!=3D</s=
pan><span style=3D"color:#000"> r</span><span style=3D"color:#660">.</span>=
<span style=3D"color:#000">q</span><span style=3D"color:#660">)</span><span=
style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span><span style=3D=
"color:#000"> l</span><span style=3D"color:#660">.</span><span style=3D"col=
or:#000">q </span><span style=3D"color:#660"><</span><span style=3D"colo=
r:#000"> r</span><span style=3D"color:#660">.</span><span style=3D"color:#0=
00">q</span><span style=3D"color:#660">;</span><span style=3D"color:#000"><=
br>
=C2=A0 </span><span style=3D"color:#008">if</span><span style=3D"color:#660=
">(</span><span style=3D"color:#000">l</span><span style=3D"color:#660">.</=
span><span style=3D"color:#000">y </span><span style=3D"color:#660">!=3D</s=
pan><span style=3D"color:#000"> r</span><span style=3D"color:#660">.</span>=
<span style=3D"color:#000">y</span><span style=3D"color:#660">)</span><span=
style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span><span style=3D=
"color:#000"> l</span><span style=3D"color:#660">.</span><span style=3D"col=
or:#000">y </span><span style=3D"color:#660"><</span><span style=3D"colo=
r:#000"> r</span><span style=3D"color:#660">.</span><span style=3D"color:#0=
00">y</span><span style=3D"color:#660">;</span><span style=3D"color:#000"><=
br>
=C2=A0 </span><span style=3D"color:#008">return</span><span style=3D"color:=
#000"> </span><span style=3D"color:#008">false</span><span style=3D"color:#=
660">;</span><span style=3D"color:#000"><br></span><span style=3D"color:#66=
0">}</span><span style=3D"color:#000"><br>
</span></div></code></div><br><br>Best Regards,<br>Ben Strasser<span class=
=3D"HOEnZb"><font color=3D"#888888"><br><br></font></span></div><span class=
=3D"HOEnZb"><font color=3D"#888888">
<p></p>
-- <br>
<br>
--- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br>
</font></span></blockquote></div><br></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--20cf303bf5e0c3684904fa9c5db6--
.
Author: Maurice Bos <m-ou.se@m-ou.se>
Date: Fri, 30 May 2014 15:04:47 +0200
Raw View
Why do you stop using a tuple in your 'without order_by' example? The
'make_tuple' trick can be used there just as well: [](S l, S r){
std::make_tuple(l.x, l.p, l.q, l.y) < std::make_tuple(r.x, r.p, r.q,
r.y); } (or std::tie, of course).
order_by(&Point::x) (member pointer) and order_by(&Point::getX)
(member function pointer) would be interesting.
2014-05-30 12:45 GMT+02:00 <strasser.ben@gmail.com>:
> Hi,
>
> (if this is a duplicate then please excuse the noise :) )
>
> I find myself commonly writing code of the following form:
>
> struct Point{
> int x,y;
> };
>
> vector<Point>vec;
> sort(vec.begin(), vec.end(), [](Point l, Point r){return l.x < r.x;});
>
>
>
> The code sorts an array of structs by one of the members. It does not matter
> in which order the y members come. To reduce the boilerplate code (and thus
> a source of hard to find typo bugs like swapping an x for a y) I use the
> following utility:
>
> template<class F>
> struct OrderBy{
> const F&f;
> template<class T>
> bool operator()(const T&l, const T&r)const{
> return f(l) < f(r);
> }
> };
> template<class F>
> OrderBy<F>order_by(const F&f){
> return {f};
> }
>
>
>
>
> This can be used as following:
>
> struct Point{
> int x,y;
> };
>
> vector<Point>vec;
> sort(vec.begin(), vec.end(), order_by([](Point p){return p.x;}));
>
>
>
> Up to now order_by is only a neat addition but nothing ground breaking. It
> really shines in combination with tuple:
>
> struct S{
> int x,y,z,p,q,r;
> };
>
> vector<S>vec;
> sort(vec.begin(), vec.end(), order_by([](S p){return make_tuple(p.x, p.p,
> p.q, p.y);}));
>
>
>
> The meaning here is: First sort by x, then by p, then by q and finally by y.
> The rest are can come in any order. Without order_by you would need
> something like:
>
> [](S l, S r){
> if(l.x != r.x)
> return l.x < r.x;
> if(l.p != r.p)
> return l.p < r.p;
> if(l.q != r.q)
> return l.q < r.q;
> if(l.y != r.y)
> return l.y < r.y;
> return false;
> }
>
>
> Best Regards,
> Ben Strasser
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> Visit this group at
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: strasser.ben@gmail.com
Date: Fri, 30 May 2014 07:01:30 -0700 (PDT)
Raw View
------=_Part_542_33365609.1401458490379
Content-Type: text/plain; charset=UTF-8
Hi,
On Friday, May 30, 2014 3:05:10 PM UTC+2, Maurice Bos wrote:
>
> Why do you stop using a tuple in your 'without order_by' example?
Because I did not think of it. :)
However, the problem of easy to write but hard to find typos remains. For
example
std::sort(vec.begin(), vec.end(), [](Point l, Point r){return tie(l.x, l.y)
< tie(r.y, r.x);});
is way too easy to write. Notice that real world std::sort implementations
do *not* test whether the order predicate actually is one (as in it fulfils
the required axioms) and therefore std::sort would produce very strange
results. This can not happen with
std::sort(vec.begin(), vec.end(), order_by([](Point p){return tie(p.x, p.y
);});
as the resulting order predicate is guaranteed to be at least valid. It is
still possible that the user messed up the order of x and y but as soon as
he looks at vec in the debugger it should become clear very quickly where
the problem is. This is not necessarily the case if std::sort just produces
garbage.
I'm uncertain whether I like order_by(Point::x). I do not think that it
hurts much as I see not much potential for conflict. However, it's
basically only syntax sugar compared to order_by([](Point p){return p.x;}).
Exactly the same terms appear in the same order. It only safes a few
characters. I do not believe that the added complexity is worth it.
A small problem I see is that many users will write code like the following
to sort decreasingly:
std::sort(vec.begin(), vec.end(), order_by([](Point p){return make_tuple(-p.
x, p.y);});
At first this seems correct but unexpected stuff happens for p.x ==
numeric_limits<int>::min() as -numeric_limits<int>::min() == 0 on common
processors. Notice that this example is already sufficiently complex to
show that adding and promoting an order_decreasing(order_by(...)) is not
enough. Further notice that this is also a situation where make_tuple is
needed and tie will not do.
Best Regards,
Ben Strasser
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_542_33365609.1401458490379
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hi,<br><br>On Friday, May 30, 2014 3:05:10 PM UTC+2, Mauri=
ce Bos wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-le=
ft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Why do you stop u=
sing a tuple in your 'without order_by' example? </blockquote><div><br>Beca=
use I did not think of it. :)<br><br>However, the problem of easy to write =
but hard to find typos remains. For example<br><br><div class=3D"prettyprin=
t" style=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 18=
7, 187); border-style: solid; border-width: 1px; word-wrap: break-word;"><c=
ode class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">std</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">sort</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify">vec</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">.</span><span style=3D"color: #008;" class=3D"styled-by-prettify">begin</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">(),</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> vec</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"co=
lor: #008;" class=3D"styled-by-prettify">end</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">(),</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">[](</span><span style=3D"color: #606;" class=3D"styled-by=
-prettify">Point</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> l</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><sp=
an style=3D"color: #606;" class=3D"styled-by-prettify">Point</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> r</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">){</span><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">return</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify"> tie</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">l</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">x</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> l</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">y</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify"><</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> tie</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify">r</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">y</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> r</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">.</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify">x</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">);});</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r></span></div></code></div><br>is way too easy to write. Notice that real =
world std::sort implementations do *not* test whether the order predicate a=
ctually is one (as in it fulfils the required axioms) and therefore std::so=
rt would produce very strange results. This can not happen with<br><br><div=
class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); borde=
r-color: rgb(187, 187, 187); border-style: solid; border-width: 1px; word-w=
rap: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"=
><span style=3D"color: #000;" class=3D"styled-by-prettify">std</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify">sort</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify">vec</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">.</span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">begin</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">(),</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> vec</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</=
span><span style=3D"color: #008;" class=3D"styled-by-prettify">end</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">(),</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> order_by</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">([](</span><span style=3D"c=
olor: #606;" class=3D"styled-by-prettify">Point</span><span style=3D"color:=
#000;" class=3D"styled-by-prettify"> p</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">){</span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">return</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> tie</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">p</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify">x</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> p</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">y</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">);});</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"><br></span></div></code></div><br>as the resulting order predicat=
e is guaranteed to be at least valid. It is still possible that the user me=
ssed up the order of x and y but as soon as he looks at vec in the debugger=
it should become clear very quickly where the problem is. This is not nece=
ssarily the case if std::sort just produces garbage.<br><br>I'm uncertain w=
hether I like order_by(Point::x). I do not think that it hurts much as I se=
e not much potential for conflict. However, it's basically only syntax suga=
r compared to order_by([](Point p){return p.x;}). Exactly the same terms ap=
pear in the same order. It only safes a few characters. I do not believe th=
at the added complexity is worth it.<br><br>A small problem I see is that m=
any users will write code like the following to sort decreasingly:<br><br><=
div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); bo=
rder-color: rgb(187, 187, 187); border-style: solid; border-width: 1px; wor=
d-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subprettypri=
nt"><span style=3D"color: #000;" class=3D"styled-by-prettify">std</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">sort</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify">vec</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">.</span><span style=3D"color: #008;" class=3D"styl=
ed-by-prettify">begin</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">(),</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> vec</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.<=
/span><span style=3D"color: #008;" class=3D"styled-by-prettify">end</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">(),</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> order_by</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">([](</span><span style=3D"=
color: #606;" class=3D"styled-by-prettify">Point</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> p</span><span style=3D"color: #660;"=
class=3D"styled-by-prettify">){</span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">return</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> make_tuple</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">(-</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify">p</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">x</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> p</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify">y</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">);});</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"><br></span></div></code></div><br>At first this seems =
correct but unexpected stuff happens for p.x =3D=3D numeric_limits<int&g=
t;::min() as -numeric_limits<int>::min() =3D=3D 0 on common processor=
s. Notice that this example is already sufficiently complex to show that ad=
ding and promoting an order_decreasing(order_by(...)) is not enough. Furthe=
r notice that this is also a situation where make_tuple is needed and tie w=
ill not do.<br><br>Best Regards,<br>Ben Strasser<br> </div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_542_33365609.1401458490379--
.
Author: "'Geoffrey Romer' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Fri, 30 May 2014 09:29:17 -0700
Raw View
--001a11c117080184b904faa08cf0
Content-Type: text/plain; charset=UTF-8
I generally really like this. I do see a couple problems, though:
- There's no obvious way to control the comparison that's applied to the
extracted values. For example, what if I want to sort in decreasing order?
- It doesn't seem to work very well when you need to name the comparator
type, e.g. to use it with an associative container.
On Fri, May 30, 2014 at 3:45 AM, <strasser.ben@gmail.com> wrote:
> Hi,
>
> (if this is a duplicate then please excuse the noise :) )
>
> I find myself commonly writing code of the following form:
>
> struct Point{
> int x,y;
> };
>
> vector<Point>vec;
> sort(vec.begin(), vec.end(), [](Point l, Point r){return l.x < r.x;});
>
>
>
> The code sorts an array of structs by one of the members. It does not
> matter in which order the y members come. To reduce the boilerplate code
> (and thus a source of hard to find typo bugs like swapping an x for a y) I
> use the following utility:
>
> template<class F>
> struct OrderBy{
> const F&f;
> template<class T>
> bool operator()(const T&l, const T&r)const{
> return f(l) < f(r);
> }
> };
> template<class F>
> OrderBy<F>order_by(const F&f){
> return {f};
> }
>
>
>
>
> This can be used as following:
>
> struct Point{
> int x,y;
> };
>
> vector<Point>vec;
> sort(vec.begin(), vec.end(), order_by([](Point p){return p.x;}));
>
>
>
> Up to now order_by is only a neat addition but nothing ground breaking. It
> really shines in combination with tuple:
>
> struct S{
> int x,y,z,p,q,r;
> };
>
> vector<S>vec;
> sort(vec.begin(), vec.end(), order_by([](S p){return make_tuple(p.x, p.p,
> p.q, p.y);}));
>
>
>
> The meaning here is: First sort by x, then by p, then by q and finally by
> y. The rest are can come in any order. Without order_by you would need
> something like:
>
> [](S l, S r){
> if(l.x != r.x)
> return l.x < r.x;
> if(l.p != r.p)
> return l.p < r.p;
> if(l.q != r.q)
> return l.q < r.q;
> if(l.y != r.y)
> return l.y < r.y;
> return false;
> }
>
>
> Best Regards,
> Ben Strasser
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> Visit this group at
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--001a11c117080184b904faa08cf0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I generally really like this. I do see a couple problems, =
though:<div>- There's no obvious way to control the comparison that'=
;s applied to the extracted values. For example, what if I want to sort in =
decreasing order?<br>
</div><div>- It doesn't seem to work very well when you need to name th=
e comparator type, e.g. to use it with an associative container.<br></div><=
/div><div class=3D"gmail_extra"><br><br><div class=3D"gmail_quote">On Fri, =
May 30, 2014 at 3:45 AM, <span dir=3D"ltr"><<a href=3D"mailto:strasser.=
ben@gmail.com" target=3D"_blank">strasser.ben@gmail.com</a>></span> wrot=
e:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div dir=3D"ltr">Hi,<br><br>(if this is a du=
plicate then please excuse the noise :) )<br><br>I find myself commonly wri=
ting code of the following form:<br>
<br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,18=
7,187);border-style:solid;border-width:1px;word-wrap:break-word"><code><div=
><span style=3D"color:#008">struct</span><span style=3D"color:#000"> </span=
><span style=3D"color:#606">Point</span><span style=3D"color:#660">{</span>=
<span style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">int</span><span style=3D"color:#00=
0"> x</span><span style=3D"color:#660">,</span><span style=3D"color:#000">y=
</span><span style=3D"color:#660">;</span><span style=3D"color:#000"><br></=
span><span style=3D"color:#660">};</span><span style=3D"color:#000"><br>
<br>vector</span><span style=3D"color:#660"><</span><span style=3D"color=
:#606">Point</span><span style=3D"color:#660">></span><span style=3D"col=
or:#000">vec</span><span style=3D"color:#660">;</span><span style=3D"color:=
#000"><br>
sort</span><span style=3D"color:#660">(</span><span style=3D"color:#000">ve=
c</span><span style=3D"color:#660">.</span><span style=3D"color:#008">begin=
</span><span style=3D"color:#660">(),</span><span style=3D"color:#000"> vec=
</span><span style=3D"color:#660">.</span><span style=3D"color:#008">end</s=
pan><span style=3D"color:#660">(),</span><span style=3D"color:#000"> </span=
><span style=3D"color:#660">[](</span><span style=3D"color:#606">Point</spa=
n><span style=3D"color:#000"> l</span><span style=3D"color:#660">,</span><s=
pan style=3D"color:#000"> </span><span style=3D"color:#606">Point</span><sp=
an style=3D"color:#000"> r</span><span style=3D"color:#660">){</span><span =
style=3D"color:#008">return</span><span style=3D"color:#000"> l</span><span=
style=3D"color:#660">.</span><span style=3D"color:#000">x </span><span sty=
le=3D"color:#660"><</span><span style=3D"color:#000"> r</span><span styl=
e=3D"color:#660">.</span><span style=3D"color:#000">x</span><span style=3D"=
color:#660">;});</span><span style=3D"color:#000"><br>
</span></div></code></div><br><br><br>The code sorts an array of structs by=
one of the members. It does not matter in which order the y members come. =
To reduce the boilerplate code (and thus a source of hard to find typo bugs=
like swapping an x for a y) I use the following utility:<br>
<br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,18=
7,187);border-style:solid;border-width:1px;word-wrap:break-word"><code><div=
><span style=3D"color:#008">template</span><span style=3D"color:#660"><<=
/span><span style=3D"color:#008">class</span><span style=3D"color:#000"> F<=
/span><span style=3D"color:#660">></span><span style=3D"color:#000"><br>
</span><span style=3D"color:#008">struct</span><span style=3D"color:#000"> =
</span><span style=3D"color:#606">OrderBy</span><span style=3D"color:#660">=
{</span><span style=3D"color:#000"><br>=C2=A0 </span><span style=3D"color:#=
008">const</span><span style=3D"color:#000"> F</span><span style=3D"color:#=
660">&</span><span style=3D"color:#000">f</span><span style=3D"color:#6=
60">;</span><span style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">template</span><span style=3D"colo=
r:#660"><</span><span style=3D"color:#008">class</span><span style=3D"co=
lor:#000"> T</span><span style=3D"color:#660">></span><span style=3D"col=
or:#000"><br>
=C2=A0 </span><span style=3D"color:#008">bool</span><span style=3D"color:#0=
00"> </span><span style=3D"color:#008">operator</span><span style=3D"color:=
#660">()(</span><span style=3D"color:#008">const</span><span style=3D"color=
:#000"> T</span><span style=3D"color:#660">&</span><span style=3D"color=
:#000">l</span><span style=3D"color:#660">,</span><span style=3D"color:#000=
"> </span><span style=3D"color:#008">const</span><span style=3D"color:#000"=
> T</span><span style=3D"color:#660">&</span><span style=3D"color:#000"=
>r</span><span style=3D"color:#660">)</span><span style=3D"color:#008">cons=
t</span><span style=3D"color:#660">{</span><span style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span><span style=3D=
"color:#000"> f</span><span style=3D"color:#660">(</span><span style=3D"col=
or:#000">l</span><span style=3D"color:#660">)</span><span style=3D"color:#0=
00"> </span><span style=3D"color:#660"><</span><span style=3D"color:#000=
"> f</span><span style=3D"color:#660">(</span><span style=3D"color:#000">r<=
/span><span style=3D"color:#660">);</span><span style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#660">}</span><span style=3D"color:#000"=
><br></span><span style=3D"color:#660">};</span><span style=3D"color:#000">=
<br></span><span style=3D"color:#008">template</span><span style=3D"color:#=
660"><</span><span style=3D"color:#008">class</span><span style=3D"color=
:#000"> F</span><span style=3D"color:#660">></span><span style=3D"color:=
#000"><br>
</span><span style=3D"color:#606">OrderBy</span><span style=3D"color:#660">=
<</span><span style=3D"color:#000">F</span><span style=3D"color:#660">&g=
t;</span><span style=3D"color:#000">order_by</span><span style=3D"color:#66=
0">(</span><span style=3D"color:#008">const</span><span style=3D"color:#000=
"> F</span><span style=3D"color:#660">&</span><span style=3D"color:#000=
">f</span><span style=3D"color:#660">){</span><span style=3D"color:#000"><b=
r>
=C2=A0 </span><span style=3D"color:#008">return</span><span style=3D"color:=
#000"> </span><span style=3D"color:#660">{</span><span style=3D"color:#000"=
>f</span><span style=3D"color:#660">};</span><span style=3D"color:#000"><br=
></span><span style=3D"color:#660">}</span><span style=3D"color:#000"><br>
<br></span></div></code></div><br><br><br>This can be used as following:<br=
><br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,1=
87,187);border-style:solid;border-width:1px;word-wrap:break-word"><code><di=
v>
<span style=3D"color:#008">struct</span><span style=3D"color:#000"> </span>=
<span style=3D"color:#606">Point</span><span style=3D"color:#660">{</span><=
span style=3D"color:#000"><br>=C2=A0 </span><span style=3D"color:#008">int<=
/span><span style=3D"color:#000"> x</span><span style=3D"color:#660">,</spa=
n><span style=3D"color:#000">y</span><span style=3D"color:#660">;</span><sp=
an style=3D"color:#000"><br>
</span><span style=3D"color:#660">};</span><span style=3D"color:#000"><br><=
br>vector</span><span style=3D"color:#660"><</span><span style=3D"color:=
#606">Point</span><span style=3D"color:#660">></span><span style=3D"colo=
r:#000">vec</span><span style=3D"color:#660">;</span><span style=3D"color:#=
000"><br>
sort</span><span style=3D"color:#660">(</span><span style=3D"color:#000">ve=
c</span><span style=3D"color:#660">.</span><span style=3D"color:#008">begin=
</span><span style=3D"color:#660">(),</span><span style=3D"color:#000"> vec=
</span><span style=3D"color:#660">.</span><span style=3D"color:#008">end</s=
pan><span style=3D"color:#660">(),</span><span style=3D"color:#000"> order_=
by</span><span style=3D"color:#660">([](</span><span style=3D"color:#606">P=
oint</span><span style=3D"color:#000"> p</span><span style=3D"color:#660">)=
{</span><span style=3D"color:#008">return</span><span style=3D"color:#000">=
p</span><span style=3D"color:#660">.</span><span style=3D"color:#000">x</s=
pan><span style=3D"color:#660">;}));</span><span style=3D"color:#000"><br>
</span></div></code></div><br><br><br>Up to now order_by is only a neat add=
ition but nothing ground breaking. It really shines in combination with tup=
le:<br><br><div style=3D"background-color:rgb(250,250,250);border-color:rgb=
(187,187,187);border-style:solid;border-width:1px;word-wrap:break-word">
<code><div><span style=3D"color:#008">struct</span><span style=3D"color:#00=
0"> S</span><span style=3D"color:#660">{</span><span style=3D"color:#000"><=
br>=C2=A0 </span><span style=3D"color:#008">int</span><span style=3D"color:=
#000"> x</span><span style=3D"color:#660">,</span><span style=3D"color:#000=
">y</span><span style=3D"color:#660">,</span><span style=3D"color:#000">z</=
span><span style=3D"color:#660">,</span><span style=3D"color:#000">p</span>=
<span style=3D"color:#660">,</span><span style=3D"color:#000">q</span><span=
style=3D"color:#660">,</span><span style=3D"color:#000">r</span><span styl=
e=3D"color:#660">;</span><span style=3D"color:#000"><br>
</span><span style=3D"color:#660">};</span><span style=3D"color:#000"><br><=
br>vector</span><span style=3D"color:#660"><</span><span style=3D"color:=
#000">S</span><span style=3D"color:#660">></span><span style=3D"color:#0=
00">vec</span><span style=3D"color:#660">;</span><span style=3D"color:#000"=
><br>
sort</span><span style=3D"color:#660">(</span><span style=3D"color:#000">ve=
c</span><span style=3D"color:#660">.</span><span style=3D"color:#008">begin=
</span><span style=3D"color:#660">(),</span><span style=3D"color:#000"> vec=
</span><span style=3D"color:#660">.</span><span style=3D"color:#008">end</s=
pan><span style=3D"color:#660">(),</span><span style=3D"color:#000"> order_=
by</span><span style=3D"color:#660">([](</span><span style=3D"color:#000">S=
p</span><span style=3D"color:#660">){</span><span style=3D"color:#008">ret=
urn</span><span style=3D"color:#000"> make_tuple</span><span style=3D"color=
:#660">(</span><span style=3D"color:#000">p</span><span style=3D"color:#660=
">.</span><span style=3D"color:#000">x</span><span style=3D"color:#660">,</=
span><span style=3D"color:#000"> p</span><span style=3D"color:#660">.</span=
><span style=3D"color:#000">p</span><span style=3D"color:#660">,</span><spa=
n style=3D"color:#000"> p</span><span style=3D"color:#660">.</span><span st=
yle=3D"color:#000">q</span><span style=3D"color:#660">,</span><span style=
=3D"color:#000"> p</span><span style=3D"color:#660">.</span><span style=3D"=
color:#000">y</span><span style=3D"color:#660">);}));</span><span style=3D"=
color:#000"><br>
</span></div></code></div><br><br><br>The meaning here is: First sort by x,=
then by p, then by q and finally by y. The rest are can come in any order.=
Without order_by you would need something like:<br><br><div style=3D"backg=
round-color:rgb(250,250,250);border-color:rgb(187,187,187);border-style:sol=
id;border-width:1px;word-wrap:break-word">
<code><div><span style=3D"color:#660">[](</span><span style=3D"color:#000">=
S l</span><span style=3D"color:#660">,</span><span style=3D"color:#000"> S =
r</span><span style=3D"color:#660">){</span><span style=3D"color:#000"><br>=
=C2=A0 </span><span style=3D"color:#008">if</span><span style=3D"color:#660=
">(</span><span style=3D"color:#000">l</span><span style=3D"color:#660">.</=
span><span style=3D"color:#000">x </span><span style=3D"color:#660">!=3D</s=
pan><span style=3D"color:#000"> r</span><span style=3D"color:#660">.</span>=
<span style=3D"color:#000">x</span><span style=3D"color:#660">)</span><span=
style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span><span style=3D=
"color:#000"> l</span><span style=3D"color:#660">.</span><span style=3D"col=
or:#000">x </span><span style=3D"color:#660"><</span><span style=3D"colo=
r:#000"> r</span><span style=3D"color:#660">.</span><span style=3D"color:#0=
00">x</span><span style=3D"color:#660">;</span><span style=3D"color:#000"><=
br>
=C2=A0 </span><span style=3D"color:#008">if</span><span style=3D"color:#660=
">(</span><span style=3D"color:#000">l</span><span style=3D"color:#660">.</=
span><span style=3D"color:#000">p </span><span style=3D"color:#660">!=3D</s=
pan><span style=3D"color:#000"> r</span><span style=3D"color:#660">.</span>=
<span style=3D"color:#000">p</span><span style=3D"color:#660">)</span><span=
style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span><span style=3D=
"color:#000"> l</span><span style=3D"color:#660">.</span><span style=3D"col=
or:#000">p </span><span style=3D"color:#660"><</span><span style=3D"colo=
r:#000"> r</span><span style=3D"color:#660">.</span><span style=3D"color:#0=
00">p</span><span style=3D"color:#660">;</span><span style=3D"color:#000"><=
br>
=C2=A0 </span><span style=3D"color:#008">if</span><span style=3D"color:#660=
">(</span><span style=3D"color:#000">l</span><span style=3D"color:#660">.</=
span><span style=3D"color:#000">q </span><span style=3D"color:#660">!=3D</s=
pan><span style=3D"color:#000"> r</span><span style=3D"color:#660">.</span>=
<span style=3D"color:#000">q</span><span style=3D"color:#660">)</span><span=
style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span><span style=3D=
"color:#000"> l</span><span style=3D"color:#660">.</span><span style=3D"col=
or:#000">q </span><span style=3D"color:#660"><</span><span style=3D"colo=
r:#000"> r</span><span style=3D"color:#660">.</span><span style=3D"color:#0=
00">q</span><span style=3D"color:#660">;</span><span style=3D"color:#000"><=
br>
=C2=A0 </span><span style=3D"color:#008">if</span><span style=3D"color:#660=
">(</span><span style=3D"color:#000">l</span><span style=3D"color:#660">.</=
span><span style=3D"color:#000">y </span><span style=3D"color:#660">!=3D</s=
pan><span style=3D"color:#000"> r</span><span style=3D"color:#660">.</span>=
<span style=3D"color:#000">y</span><span style=3D"color:#660">)</span><span=
style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span><span style=3D=
"color:#000"> l</span><span style=3D"color:#660">.</span><span style=3D"col=
or:#000">y </span><span style=3D"color:#660"><</span><span style=3D"colo=
r:#000"> r</span><span style=3D"color:#660">.</span><span style=3D"color:#0=
00">y</span><span style=3D"color:#660">;</span><span style=3D"color:#000"><=
br>
=C2=A0 </span><span style=3D"color:#008">return</span><span style=3D"color:=
#000"> </span><span style=3D"color:#008">false</span><span style=3D"color:#=
660">;</span><span style=3D"color:#000"><br></span><span style=3D"color:#66=
0">}</span><span style=3D"color:#000"><br>
</span></div></code></div><br><br>Best Regards,<br>Ben Strasser<span class=
=3D"HOEnZb"><font color=3D"#888888"><br><br></font></span></div><span class=
=3D"HOEnZb"><font color=3D"#888888">
<p></p>
-- <br>
<br>
--- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br>
</font></span></blockquote></div><br></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--001a11c117080184b904faa08cf0--
.
Author: Andrew Tomazos <andrewtomazos@gmail.com>
Date: Fri, 30 May 2014 19:03:06 +0200
Raw View
--047d7bd760c2f61ab504faa104ed
Content-Type: text/plain; charset=UTF-8
On Fri, May 30, 2014 at 6:29 PM, 'Geoffrey Romer' via ISO C++ Standard -
Future Proposals <std-proposals@isocpp.org> wrote:
> I generally really like this. I do see a couple problems, though:
> - There's no obvious way to control the comparison that's applied to the
> extracted values. For example, what if I want to sort in decreasing order?
> - It doesn't seem to work very well when you need to name the comparator
> type, e.g. to use it with an associative container.
>
To your first point, I was going to suggest that the second parameter to
order_by be a comparator that defaults to std::less, but the problem is the
same as the second point, it seems as though you would have to name the
type-id:
sort(vec.begin(), vec.end(), order_by([](const S& p){return tie(p.x, p.p
, p.q, p.y)}, std::greater<std::tuple<int&, int&, int&, int&>>);
I'd rather write:
sort(vec.begin(), vec.end(), order_by([](const S& p){return tie(p.x, p.p
, p.q, p.y)}, std::greater);
Like instead of:
sort(vec.begin(), vec.end(), std::greater<int>);
I would rather write:
sort(vec.begin(), vec.end(), std::greater);
(actually I would rather write:
sort(vec, std::greater);
but that's a different story.)
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--047d7bd760c2f61ab504faa104ed
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On F=
ri, May 30, 2014 at 6:29 PM, 'Geoffrey Romer' via ISO C++ Standard =
- Future Proposals <span dir=3D"ltr"><<a href=3D"mailto:std-proposals@is=
ocpp.org" target=3D"_blank">std-proposals@isocpp.org</a>></span> wrote:<=
br>
<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-=
left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;p=
adding-left:1ex"><div dir=3D"ltr">I generally really like this. I do see a =
couple problems, though:<div>
- There's no obvious way to control the comparison that's applied t=
o the extracted values. For example, what if I want to sort in decreasing o=
rder?<br>
</div><div>- It doesn't seem to work very well when you need to name th=
e comparator type, e.g. to use it with an associative container.</div></div=
></blockquote><div><br></div><div>To your first point, I was going to sugge=
st that the second parameter to order_by be a comparator that defaults to s=
td::less, but the problem is the same as the second point, it seems as thou=
gh you would have to name the type-id:</div>
<div><br></div><div><span style=3D"font-family:monospace;font-size:10px;col=
or:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0 =C2=A0 sort</span><=
span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);bac=
kground-color:rgb(250,250,250)">(</span><span style=3D"font-family:monospac=
e;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">vec</s=
pan><span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0=
);background-color:rgb(250,250,250)">.</span><span style=3D"font-family:mon=
ospace;font-size:10px;color:rgb(0,0,136);background-color:rgb(250,250,250)"=
>begin</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(=
102,102,0);background-color:rgb(250,250,250)">(),</span><span style=3D"font=
-family:monospace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,=
250,250)">=C2=A0vec</span><span style=3D"font-family:monospace;font-size:10=
px;color:rgb(102,102,0);background-color:rgb(250,250,250)">.</span><span st=
yle=3D"font-family:monospace;font-size:10px;color:rgb(0,0,136);background-c=
olor:rgb(250,250,250)">end</span><span style=3D"font-family:monospace;font-=
size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">(),</span=
><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);backg=
round-color:rgb(250,250,250)">=C2=A0order_by</span><span style=3D"font-fami=
ly:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(250,2=
50,250)">([](const=C2=A0</span><span style=3D"font-family:monospace;font-si=
ze:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">S& p</span>=
<span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);ba=
ckground-color:rgb(250,250,250)">){</span><span style=3D"font-family:monosp=
ace;font-size:10px;color:rgb(0,0,136);background-color:rgb(250,250,250)">re=
turn</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,=
0,0);background-color:rgb(250,250,250)">=C2=A0tie</span><span style=3D"font=
-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(=
250,250,250)">(</span><span style=3D"font-family:monospace;font-size:10px;c=
olor:rgb(0,0,0);background-color:rgb(250,250,250)">p</span><span style=3D"f=
ont-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:r=
gb(250,250,250)">.</span><span style=3D"font-family:monospace;font-size:10p=
x;color:rgb(0,0,0);background-color:rgb(250,250,250)">x</span><span style=
=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);background-co=
lor:rgb(250,250,250)">,</span><span style=3D"font-family:monospace;font-siz=
e:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0p</span><s=
pan style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);back=
ground-color:rgb(250,250,250)">.</span><span style=3D"font-family:monospace=
;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">p</span=
><span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);b=
ackground-color:rgb(250,250,250)">,</span><span style=3D"font-family:monosp=
ace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=
=A0p</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(10=
2,102,0);background-color:rgb(250,250,250)">.</span><span style=3D"font-fam=
ily:monospace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,=
250)">q</span><span style=3D"font-family:monospace;font-size:10px;color:rgb=
(102,102,0);background-color:rgb(250,250,250)">,</span><span style=3D"font-=
family:monospace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,2=
50,250)">=C2=A0p</span><span style=3D"font-family:monospace;font-size:10px;=
color:rgb(102,102,0);background-color:rgb(250,250,250)">.</span><span style=
=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);background-color:=
rgb(250,250,250)">y</span><span style=3D"font-family:monospace;font-size:10=
px;background-color:rgb(250,250,250)"><font color=3D"#666600">)}, std::grea=
ter<std::tuple<int&, int&, int&, int&>>);</font=
></span><br>
</div><div><span style=3D"font-family:monospace;font-size:10px;background-c=
olor:rgb(250,250,250)"><font color=3D"#666600"><br></font></span></div><div=
><span style=3D"font-family:monospace;font-size:10px;background-color:rgb(2=
50,250,250)"><font color=3D"#666600">I'd rather write:</font></span></d=
iv>
<div><span style=3D"font-family:monospace;font-size:10px;background-color:r=
gb(250,250,250)"><font color=3D"#666600"><br></font></span></div><div><div>=
<span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);backgr=
ound-color:rgb(250,250,250)">=C2=A0 =C2=A0 sort</span><span style=3D"font-f=
amily:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(25=
0,250,250)">(</span><span style=3D"font-family:monospace;font-size:10px;col=
or:rgb(0,0,0);background-color:rgb(250,250,250)">vec</span><span style=3D"f=
ont-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:r=
gb(250,250,250)">.</span><span style=3D"font-family:monospace;font-size:10p=
x;color:rgb(0,0,136);background-color:rgb(250,250,250)">begin</span><span s=
tyle=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);backgroun=
d-color:rgb(250,250,250)">(),</span><span style=3D"font-family:monospace;fo=
nt-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0vec<=
/span><span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102=
,0);background-color:rgb(250,250,250)">.</span><span style=3D"font-family:m=
onospace;font-size:10px;color:rgb(0,0,136);background-color:rgb(250,250,250=
)">end</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(=
102,102,0);background-color:rgb(250,250,250)">(),</span><span style=3D"font=
-family:monospace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,=
250,250)">=C2=A0order_by</span><span style=3D"font-family:monospace;font-si=
ze:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">([](const=
=C2=A0</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(=
0,0,0);background-color:rgb(250,250,250)">S& p</span><span style=3D"fon=
t-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb=
(250,250,250)">){</span><span style=3D"font-family:monospace;font-size:10px=
;color:rgb(0,0,136);background-color:rgb(250,250,250)">return</span><span s=
tyle=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);background-co=
lor:rgb(250,250,250)">=C2=A0tie</span><span style=3D"font-family:monospace;=
font-size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">(</s=
pan><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);ba=
ckground-color:rgb(250,250,250)">p</span><span style=3D"font-family:monospa=
ce;font-size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">.=
</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0)=
;background-color:rgb(250,250,250)">x</span><span style=3D"font-family:mono=
space;font-size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)=
">,</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0=
,0);background-color:rgb(250,250,250)">=C2=A0p</span><span style=3D"font-fa=
mily:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(250=
,250,250)">.</span><span style=3D"font-family:monospace;font-size:10px;colo=
r:rgb(0,0,0);background-color:rgb(250,250,250)">p</span><span style=3D"font=
-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(=
250,250,250)">,</span><span style=3D"font-family:monospace;font-size:10px;c=
olor:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0p</span><span styl=
e=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);background-c=
olor:rgb(250,250,250)">.</span><span style=3D"font-family:monospace;font-si=
ze:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">q</span><span s=
tyle=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);backgroun=
d-color:rgb(250,250,250)">,</span><span style=3D"font-family:monospace;font=
-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0p</spa=
n><span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);=
background-color:rgb(250,250,250)">.</span><span style=3D"font-family:monos=
pace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">y</=
span><span style=3D"font-family:monospace;font-size:10px;background-color:r=
gb(250,250,250)"><font color=3D"#666600">)}, std::greater);</font></span><b=
r>
</div></div><div><span style=3D"font-family:monospace;font-size:10px;backgr=
ound-color:rgb(250,250,250)"><font color=3D"#666600"><br></font></span></di=
v><div><span style=3D"font-family:monospace;font-size:10px;background-color=
:rgb(250,250,250)"><font color=3D"#666600">Like instead of:</font></span></=
div>
<div><br></div><div><div><span style=3D"font-family:monospace;font-size:10p=
x;background-color:rgb(250,250,250)"><font color=3D"#666600">=C2=A0 =C2=A0 =
sort(vec.begin(), vec.end(), std::greater<int>);</font></span></div><=
/div><div>
<span style=3D"color:rgb(102,102,0);font-family:monospace;font-size:10px;ba=
ckground-color:rgb(250,250,250)"><br></span></div><div><span style=3D"color=
:rgb(102,102,0);font-family:monospace;font-size:10px;background-color:rgb(2=
50,250,250)">I would rather write:</span><br>
</div><div><span style=3D"font-family:monospace;font-size:10px;background-c=
olor:rgb(250,250,250)"><font color=3D"#666600"><br></font></span></div><div=
><span style=3D"font-family:monospace;font-size:10px;background-color:rgb(2=
50,250,250)"><font color=3D"#666600">=C2=A0 =C2=A0 sort(vec.begin(), vec.en=
d(), std::greater);</font></span></div>
<div><br></div><div><span style=3D"font-family:monospace;font-size:10px;bac=
kground-color:rgb(250,250,250)"><font color=3D"#666600">(actually I would r=
ather write:</font></span></div><div><span style=3D"font-family:monospace;f=
ont-size:10px;background-color:rgb(250,250,250)"><font color=3D"#666600"><b=
r>
</font></span></div><div><span style=3D"font-family:monospace;font-size:10p=
x;background-color:rgb(250,250,250)"><font color=3D"#666600">=C2=A0 =C2=A0 =
sort(vec, std::greater);</font></span></div><div><span style=3D"font-family=
:monospace;font-size:10px;background-color:rgb(250,250,250)"><font color=3D=
"#666600"><br>
</font></span></div><div><span style=3D"font-family:monospace;font-size:10p=
x;background-color:rgb(250,250,250)"><font color=3D"#666600">but that's=
a different story.)</font></span></div><div><span style=3D"font-family:mon=
ospace;font-size:10px;background-color:rgb(250,250,250)"><font color=3D"#66=
6600"><br>
</font></span></div><div><span style=3D"font-family:monospace;font-size:10p=
x;background-color:rgb(250,250,250)"><font color=3D"#666600"><br></font></s=
pan></div></div></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--047d7bd760c2f61ab504faa104ed--
.
Author: Andrew Tomazos <andrewtomazos@gmail.com>
Date: Fri, 30 May 2014 19:16:42 +0200
Raw View
--047d7bd760c296bd8a04faa135c4
Content-Type: text/plain; charset=UTF-8
Of course, this whole business can be done a different way::
auto key = [](const S&p) { return tie(p.x, p.p, p.q, p.y); }
auto cmp = [&](const S& p, const S& q) { return key(p) > key(q); }
sort(vec.begin(), vec.end(), cmp);
On Fri, May 30, 2014 at 7:03 PM, Andrew Tomazos <andrewtomazos@gmail.com>
wrote:
> On Fri, May 30, 2014 at 6:29 PM, 'Geoffrey Romer' via ISO C++ Standard -
> Future Proposals <std-proposals@isocpp.org> wrote:
>
>> I generally really like this. I do see a couple problems, though:
>> - There's no obvious way to control the comparison that's applied to the
>> extracted values. For example, what if I want to sort in decreasing order?
>> - It doesn't seem to work very well when you need to name the comparator
>> type, e.g. to use it with an associative container.
>>
>
> To your first point, I was going to suggest that the second parameter to
> order_by be a comparator that defaults to std::less, but the problem is the
> same as the second point, it seems as though you would have to name the
> type-id:
>
> sort(vec.begin(), vec.end(), order_by([](const S& p){return tie(p.x, p
> .p, p.q, p.y)}, std::greater<std::tuple<int&, int&, int&, int&>>);
>
> I'd rather write:
>
> sort(vec.begin(), vec.end(), order_by([](const S& p){return tie(p.x, p
> .p, p.q, p.y)}, std::greater);
>
> Like instead of:
>
> sort(vec.begin(), vec.end(), std::greater<int>);
>
> I would rather write:
>
> sort(vec.begin(), vec.end(), std::greater);
>
> (actually I would rather write:
>
> sort(vec, std::greater);
>
> but that's a different story.)
>
>
>
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--047d7bd760c296bd8a04faa135c4
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Of course, this whole business can be done a different way=
::<div><br></div><div>=C2=A0 =C2=A0auto key =3D [](const S&p) { return =
tie(p.x, p.p, p.q, p.y); }</div><div>=C2=A0 =C2=A0auto cmp =3D [&](cons=
t S& p, const S& q) { return key(p) > key(q); }</div>
<div><div class=3D"gmail_extra">=C2=A0 =C2=A0sort(vec.begin(), vec.end(), c=
mp);<br><br><br><div class=3D"gmail_quote">On Fri, May 30, 2014 at 7:03 PM,=
Andrew Tomazos <span dir=3D"ltr"><<a href=3D"mailto:andrewtomazos@gmail=
..com" target=3D"_blank">andrewtomazos@gmail.com</a>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div class=3D"gmail_extra">=
<div class=3D"gmail_quote"><div class=3D"">On Fri, May 30, 2014 at 6:29 PM,=
'Geoffrey Romer' via ISO C++ Standard - Future Proposals <span dir=
=3D"ltr"><<a href=3D"mailto:std-proposals@isocpp.org" target=3D"_blank">=
std-proposals@isocpp.org</a>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-=
left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;p=
adding-left:1ex"><div dir=3D"ltr">I generally really like this. I do see a =
couple problems, though:<div>
- There's no obvious way to control the comparison that's applied t=
o the extracted values. For example, what if I want to sort in decreasing o=
rder?<br>
</div><div>- It doesn't seem to work very well when you need to name th=
e comparator type, e.g. to use it with an associative container.</div></div=
></blockquote><div><br></div></div><div>To your first point, I was going to=
suggest that the second parameter to order_by be a comparator that default=
s to std::less, but the problem is the same as the second point, it seems a=
s though you would have to name the type-id:</div>
<div><br></div><div><span style=3D"font-family:monospace;font-size:10px;col=
or:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0 =C2=A0 sort</span><=
span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);bac=
kground-color:rgb(250,250,250)">(</span><span style=3D"font-family:monospac=
e;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">vec</s=
pan><span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0=
);background-color:rgb(250,250,250)">.</span><span style=3D"font-family:mon=
ospace;font-size:10px;color:rgb(0,0,136);background-color:rgb(250,250,250)"=
>begin</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(=
102,102,0);background-color:rgb(250,250,250)">(),</span><span style=3D"font=
-family:monospace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,=
250,250)">=C2=A0vec</span><span style=3D"font-family:monospace;font-size:10=
px;color:rgb(102,102,0);background-color:rgb(250,250,250)">.</span><span st=
yle=3D"font-family:monospace;font-size:10px;color:rgb(0,0,136);background-c=
olor:rgb(250,250,250)">end</span><span style=3D"font-family:monospace;font-=
size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">(),</span=
><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);backg=
round-color:rgb(250,250,250)">=C2=A0order_by</span><span style=3D"font-fami=
ly:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(250,2=
50,250)">([](const=C2=A0</span><span style=3D"font-family:monospace;font-si=
ze:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">S& p</span>=
<span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);ba=
ckground-color:rgb(250,250,250)">){</span><span style=3D"font-family:monosp=
ace;font-size:10px;color:rgb(0,0,136);background-color:rgb(250,250,250)">re=
turn</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,=
0,0);background-color:rgb(250,250,250)">=C2=A0tie</span><span style=3D"font=
-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(=
250,250,250)">(</span><span style=3D"font-family:monospace;font-size:10px;c=
olor:rgb(0,0,0);background-color:rgb(250,250,250)">p</span><span style=3D"f=
ont-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:r=
gb(250,250,250)">.</span><span style=3D"font-family:monospace;font-size:10p=
x;color:rgb(0,0,0);background-color:rgb(250,250,250)">x</span><span style=
=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);background-co=
lor:rgb(250,250,250)">,</span><span style=3D"font-family:monospace;font-siz=
e:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0p</span><s=
pan style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);back=
ground-color:rgb(250,250,250)">.</span><span style=3D"font-family:monospace=
;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">p</span=
><span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);b=
ackground-color:rgb(250,250,250)">,</span><span style=3D"font-family:monosp=
ace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=
=A0p</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(10=
2,102,0);background-color:rgb(250,250,250)">.</span><span style=3D"font-fam=
ily:monospace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,=
250)">q</span><span style=3D"font-family:monospace;font-size:10px;color:rgb=
(102,102,0);background-color:rgb(250,250,250)">,</span><span style=3D"font-=
family:monospace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,2=
50,250)">=C2=A0p</span><span style=3D"font-family:monospace;font-size:10px;=
color:rgb(102,102,0);background-color:rgb(250,250,250)">.</span><span style=
=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);background-color:=
rgb(250,250,250)">y</span><span style=3D"font-family:monospace;font-size:10=
px;background-color:rgb(250,250,250)"><font color=3D"#666600">)}, std::grea=
ter<std::tuple<int&, int&, int&, int&>>);</font=
></span><br>
</div><div><span style=3D"font-family:monospace;font-size:10px;background-c=
olor:rgb(250,250,250)"><font color=3D"#666600"><br></font></span></div><div=
><span style=3D"font-family:monospace;font-size:10px;background-color:rgb(2=
50,250,250)"><font color=3D"#666600">I'd rather write:</font></span></d=
iv>
<div><span style=3D"font-family:monospace;font-size:10px;background-color:r=
gb(250,250,250)"><font color=3D"#666600"><br></font></span></div><div><div>=
<span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);backgr=
ound-color:rgb(250,250,250)">=C2=A0 =C2=A0 sort</span><span style=3D"font-f=
amily:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(25=
0,250,250)">(</span><span style=3D"font-family:monospace;font-size:10px;col=
or:rgb(0,0,0);background-color:rgb(250,250,250)">vec</span><span style=3D"f=
ont-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:r=
gb(250,250,250)">.</span><span style=3D"font-family:monospace;font-size:10p=
x;color:rgb(0,0,136);background-color:rgb(250,250,250)">begin</span><span s=
tyle=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);backgroun=
d-color:rgb(250,250,250)">(),</span><span style=3D"font-family:monospace;fo=
nt-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0vec<=
/span><span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102=
,0);background-color:rgb(250,250,250)">.</span><span style=3D"font-family:m=
onospace;font-size:10px;color:rgb(0,0,136);background-color:rgb(250,250,250=
)">end</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(=
102,102,0);background-color:rgb(250,250,250)">(),</span><span style=3D"font=
-family:monospace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,=
250,250)">=C2=A0order_by</span><span style=3D"font-family:monospace;font-si=
ze:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">([](const=
=C2=A0</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(=
0,0,0);background-color:rgb(250,250,250)">S& p</span><span style=3D"fon=
t-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb=
(250,250,250)">){</span><span style=3D"font-family:monospace;font-size:10px=
;color:rgb(0,0,136);background-color:rgb(250,250,250)">return</span><span s=
tyle=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);background-co=
lor:rgb(250,250,250)">=C2=A0tie</span><span style=3D"font-family:monospace;=
font-size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">(</s=
pan><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);ba=
ckground-color:rgb(250,250,250)">p</span><span style=3D"font-family:monospa=
ce;font-size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">.=
</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0)=
;background-color:rgb(250,250,250)">x</span><span style=3D"font-family:mono=
space;font-size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)=
">,</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0=
,0);background-color:rgb(250,250,250)">=C2=A0p</span><span style=3D"font-fa=
mily:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(250=
,250,250)">.</span><span style=3D"font-family:monospace;font-size:10px;colo=
r:rgb(0,0,0);background-color:rgb(250,250,250)">p</span><span style=3D"font=
-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(=
250,250,250)">,</span><span style=3D"font-family:monospace;font-size:10px;c=
olor:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0p</span><span styl=
e=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);background-c=
olor:rgb(250,250,250)">.</span><span style=3D"font-family:monospace;font-si=
ze:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">q</span><span s=
tyle=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);backgroun=
d-color:rgb(250,250,250)">,</span><span style=3D"font-family:monospace;font=
-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0p</spa=
n><span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);=
background-color:rgb(250,250,250)">.</span><span style=3D"font-family:monos=
pace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">y</=
span><span style=3D"font-family:monospace;font-size:10px;background-color:r=
gb(250,250,250)"><font color=3D"#666600">)}, std::greater);</font></span><b=
r>
</div></div><div><span style=3D"font-family:monospace;font-size:10px;backgr=
ound-color:rgb(250,250,250)"><font color=3D"#666600"><br></font></span></di=
v><div><span style=3D"font-family:monospace;font-size:10px;background-color=
:rgb(250,250,250)"><font color=3D"#666600">Like instead of:</font></span></=
div>
<div><br></div><div><div><span style=3D"font-family:monospace;font-size:10p=
x;background-color:rgb(250,250,250)"><font color=3D"#666600">=C2=A0 =C2=A0 =
sort(vec.begin(), vec.end(), std::greater<int>);</font></span></div><=
/div><div>
<span style=3D"color:rgb(102,102,0);font-family:monospace;font-size:10px;ba=
ckground-color:rgb(250,250,250)"><br></span></div><div><span style=3D"color=
:rgb(102,102,0);font-family:monospace;font-size:10px;background-color:rgb(2=
50,250,250)">I would rather write:</span><br>
</div><div><span style=3D"font-family:monospace;font-size:10px;background-c=
olor:rgb(250,250,250)"><font color=3D"#666600"><br></font></span></div><div=
><span style=3D"font-family:monospace;font-size:10px;background-color:rgb(2=
50,250,250)"><font color=3D"#666600">=C2=A0 =C2=A0 sort(vec.begin(), vec.en=
d(), std::greater);</font></span></div>
<div><br></div><div><span style=3D"font-family:monospace;font-size:10px;bac=
kground-color:rgb(250,250,250)"><font color=3D"#666600">(actually I would r=
ather write:</font></span></div><div><span style=3D"font-family:monospace;f=
ont-size:10px;background-color:rgb(250,250,250)"><font color=3D"#666600"><b=
r>
</font></span></div><div><span style=3D"font-family:monospace;font-size:10p=
x;background-color:rgb(250,250,250)"><font color=3D"#666600">=C2=A0 =C2=A0 =
sort(vec, std::greater);</font></span></div><div><span style=3D"font-family=
:monospace;font-size:10px;background-color:rgb(250,250,250)"><font color=3D=
"#666600"><br>
</font></span></div><div><span style=3D"font-family:monospace;font-size:10p=
x;background-color:rgb(250,250,250)"><font color=3D"#666600">but that's=
a different story.)</font></span></div><div><span style=3D"font-family:mon=
ospace;font-size:10px;background-color:rgb(250,250,250)"><font color=3D"#66=
6600"><br>
</font></span></div><div><span style=3D"font-family:monospace;font-size:10p=
x;background-color:rgb(250,250,250)"><font color=3D"#666600"><br></font></s=
pan></div></div></div></div>
</blockquote></div><br></div></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--047d7bd760c296bd8a04faa135c4--
.
Author: Miro Knejp <miro@knejp.de>
Date: Fri, 30 May 2014 19:21:27 +0200
Raw View
This is a multi-part message in MIME format.
--------------070509090906090703060604
Content-Type: text/plain; charset=UTF-8; format=flowed
What you are really doing is applying a projection of one domain to
another before the actual predicate. This turns out to be nothing other
than function composition:
pred(a, b)
pred(f(a), f(b))
pred(f(g(a)), f(g(b)))
....
It would be useful if the existing predicates (less<>, greater<>, etc)
could be altered in a way to allow easy function composition. Maybe
something like (warning, bikeshed ahead) less_proj(Fn f) returning a
less<T> with T deduced from Fn which applies f to all arguments before
the actual comparison f(a) < f(b). One could provide an overload taking
a pointer-to-member as convenience to perform a.*m < b.*m.
Am 30.05.2014 18:29, schrieb 'Geoffrey Romer' via ISO C++ Standard -
Future Proposals:
> I generally really like this. I do see a couple problems, though:
> - There's no obvious way to control the comparison that's applied to
> the extracted values. For example, what if I want to sort in
> decreasing order?
> - It doesn't seem to work very well when you need to name the
> comparator type, e.g. to use it with an associative container.
>
>
> On Fri, May 30, 2014 at 3:45 AM, <strasser.ben@gmail.com
> <mailto:strasser.ben@gmail.com>> wrote:
>
> Hi,
>
> (if this is a duplicate then please excuse the noise :) )
>
> I find myself commonly writing code of the following form:
>
> |
> structPoint{
> intx,y;
> };
>
> vector<Point>vec;
> sort(vec.begin(),vec.end(),[](Pointl,Pointr){returnl.x <r.x;});
> |
>
>
>
> The code sorts an array of structs by one of the members. It does
> not matter in which order the y members come. To reduce the
> boilerplate code (and thus a source of hard to find typo bugs like
> swapping an x for a y) I use the following utility:
>
> |
> template<classF>
> structOrderBy{
> constF&f;
> template<classT>
> booloperator()(constT&l,constT&r)const{
> returnf(l)<f(r);
> }
> };
> template<classF>
> OrderBy<F>order_by(constF&f){
> return{f};
> }
>
> |
>
>
>
> This can be used as following:
>
> |
> structPoint{
> intx,y;
> };
>
> vector<Point>vec;
> sort(vec.begin(),vec.end(),order_by([](Pointp){returnp.x;}));
> |
>
>
>
> Up to now order_by is only a neat addition but nothing ground
> breaking. It really shines in combination with tuple:
>
> |
> structS{
> intx,y,z,p,q,r;
> };
>
> vector<S>vec;
> sort(vec.begin(),vec.end(),order_by([](S
> p){returnmake_tuple(p.x,p.p,p.q,p.y);}));
> |
>
>
>
> The meaning here is: First sort by x, then by p, then by q and
> finally by y. The rest are can come in any order. Without order_by
> you would need something like:
>
> |
> [](S l,S r){
> if(l.x !=r.x)
> returnl.x <r.x;
> if(l.p !=r.p)
> returnl.p <r.p;
> if(l.q !=r.q)
> returnl.q <r.q;
> if(l.y !=r.y)
> returnl.y <r.y;
> returnfalse;
> }
> |
>
>
> Best Regards,
> Ben Strasser
>
> --
>
> ---
> You received this message because you are subscribed to the Google
> Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to std-proposals+unsubscribe@isocpp.org
> <mailto:std-proposals+unsubscribe@isocpp.org>.
> To post to this group, send email to std-proposals@isocpp.org
> <mailto:std-proposals@isocpp.org>.
> Visit this group at
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>
>
> --
>
> ---
> You received this message because you are subscribed to the Google
> Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to std-proposals+unsubscribe@isocpp.org
> <mailto:std-proposals+unsubscribe@isocpp.org>.
> To post to this group, send email to std-proposals@isocpp.org
> <mailto:std-proposals@isocpp.org>.
> Visit this group at
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--------------070509090906090703060604
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<html>
<head>
<meta content=3D"text/html; charset=3DUTF-8" http-equiv=3D"Content-Type=
">
</head>
<body text=3D"#000000" bgcolor=3D"#FFFFFF">
What you are really doing is applying a projection of one domain to
another before the actual predicate. This turns out to be nothing
other than function composition:<br>
<br>
pred(a, b)<br>
pred(f(a), f(b))<br>
pred(f(g(a)), f(g(b)))<br>
...<br>
<br>
It would be useful if the existing predicates (less<>,
greater<>, etc) could be altered in a way to allow easy
function composition. Maybe something like (warning, bikeshed ahead)
less_proj(Fn f) returning a less<T> with T deduced from Fn
which applies f to all arguments before the actual comparison f(a)
< f(b). One could provide an overload taking a pointer-to-member
as convenience to perform a.*m < b.*m.<br>
<br>
<div class=3D"moz-cite-prefix">Am 30.05.2014 18:29, schrieb 'Geoffrey
Romer' via ISO C++ Standard - Future Proposals:<br>
</div>
<blockquote
cite=3D"mid:CA+cyFguPi16LnunwjYr8X-N-3SPYyZ21sQFnMxtY6koBmC6+Mw@mail.gmail.=
com"
type=3D"cite">
<div dir=3D"ltr">I generally really like this. I do see a couple
problems, though:
<div>- There's no obvious way to control the comparison that's
applied to the extracted values. For example, what if I want
to sort in decreasing order?<br>
</div>
<div>- It doesn't seem to work very well when you need to name
the comparator type, e.g. to use it with an associative
container.<br>
</div>
</div>
<div class=3D"gmail_extra"><br>
<br>
<div class=3D"gmail_quote">On Fri, May 30, 2014 at 3:45 AM, <span
dir=3D"ltr"><<a moz-do-not-send=3D"true"
href=3D"mailto:strasser.ben@gmail.com" target=3D"_blank">stra=
sser.ben@gmail.com</a>></span>
wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir=3D"ltr">Hi,<br>
<br>
(if this is a duplicate then please excuse the noise :) )<br>
<br>
I find myself commonly writing code of the following form:<br=
>
<br>
<div
style=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);bo=
rder-style:solid;border-width:1px;word-wrap:break-word"><code>
<div><span style=3D"color:#008">struct</span><span
style=3D"color:#000"> </span><span
style=3D"color:#606">Point</span><span
style=3D"color:#660">{</span><span
style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">int</span><s=
pan
style=3D"color:#000"> x</span><span
style=3D"color:#660">,</span><span
style=3D"color:#000">y</span><span
style=3D"color:#660">;</span><span
style=3D"color:#000"><br>
</span><span style=3D"color:#660">};</span><span
style=3D"color:#000"><br>
<br>
vector</span><span style=3D"color:#660"><</span><s=
pan
style=3D"color:#606">Point</span><span
style=3D"color:#660">></span><span
style=3D"color:#000">vec</span><span
style=3D"color:#660">;</span><span
style=3D"color:#000"><br>
sort</span><span style=3D"color:#660">(</span><span
style=3D"color:#000">vec</span><span
style=3D"color:#660">.</span><span
style=3D"color:#008">begin</span><span
style=3D"color:#660">(),</span><span
style=3D"color:#000"> vec</span><span
style=3D"color:#660">.</span><span
style=3D"color:#008">end</span><span
style=3D"color:#660">(),</span><span
style=3D"color:#000"> </span><span
style=3D"color:#660">[](</span><span
style=3D"color:#606">Point</span><span
style=3D"color:#000"> l</span><span
style=3D"color:#660">,</span><span
style=3D"color:#000"> </span><span
style=3D"color:#606">Point</span><span
style=3D"color:#000"> r</span><span
style=3D"color:#660">){</span><span
style=3D"color:#008">return</span><span
style=3D"color:#000"> l</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">x </span><span
style=3D"color:#660"><</span><span
style=3D"color:#000"> r</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">x</span><span
style=3D"color:#660">;});</span><span
style=3D"color:#000"><br>
</span></div>
</code></div>
<br>
<br>
<br>
The code sorts an array of structs by one of the members.
It does not matter in which order the y members come. To
reduce the boilerplate code (and thus a source of hard to
find typo bugs like swapping an x for a y) I use the
following utility:<br>
<br>
<div
style=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);bo=
rder-style:solid;border-width:1px;word-wrap:break-word"><code>
<div><span style=3D"color:#008">template</span><span
style=3D"color:#660"><</span><span
style=3D"color:#008">class</span><span
style=3D"color:#000"> F</span><span
style=3D"color:#660">></span><span
style=3D"color:#000"><br>
</span><span style=3D"color:#008">struct</span><span
style=3D"color:#000"> </span><span
style=3D"color:#606">OrderBy</span><span
style=3D"color:#660">{</span><span
style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">const</span>=
<span
style=3D"color:#000"> F</span><span
style=3D"color:#660">&</span><span
style=3D"color:#000">f</span><span
style=3D"color:#660">;</span><span
style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">template</sp=
an><span
style=3D"color:#660"><</span><span
style=3D"color:#008">class</span><span
style=3D"color:#000"> T</span><span
style=3D"color:#660">></span><span
style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">bool</span><=
span
style=3D"color:#000"> </span><span
style=3D"color:#008">operator</span><span
style=3D"color:#660">()(</span><span
style=3D"color:#008">const</span><span
style=3D"color:#000"> T</span><span
style=3D"color:#660">&</span><span
style=3D"color:#000">l</span><span
style=3D"color:#660">,</span><span
style=3D"color:#000"> </span><span
style=3D"color:#008">const</span><span
style=3D"color:#000"> T</span><span
style=3D"color:#660">&</span><span
style=3D"color:#000">r</span><span
style=3D"color:#660">)</span><span
style=3D"color:#008">const</span><span
style=3D"color:#660">{</span><span
style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">retur=
n</span><span
style=3D"color:#000"> f</span><span
style=3D"color:#660">(</span><span
style=3D"color:#000">l</span><span
style=3D"color:#660">)</span><span
style=3D"color:#000"> </span><span
style=3D"color:#660"><</span><span
style=3D"color:#000"> f</span><span
style=3D"color:#660">(</span><span
style=3D"color:#000">r</span><span
style=3D"color:#660">);</span><span
style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#660">}</span><spa=
n
style=3D"color:#000"><br>
</span><span style=3D"color:#660">};</span><span
style=3D"color:#000"><br>
</span><span style=3D"color:#008">template</span><span
style=3D"color:#660"><</span><span
style=3D"color:#008">class</span><span
style=3D"color:#000"> F</span><span
style=3D"color:#660">></span><span
style=3D"color:#000"><br>
</span><span style=3D"color:#606">OrderBy</span><span
style=3D"color:#660"><</span><span
style=3D"color:#000">F</span><span
style=3D"color:#660">></span><span
style=3D"color:#000">order_by</span><span
style=3D"color:#660">(</span><span
style=3D"color:#008">const</span><span
style=3D"color:#000"> F</span><span
style=3D"color:#660">&</span><span
style=3D"color:#000">f</span><span
style=3D"color:#660">){</span><span
style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">return</span=
><span
style=3D"color:#000"> </span><span
style=3D"color:#660">{</span><span
style=3D"color:#000">f</span><span
style=3D"color:#660">};</span><span
style=3D"color:#000"><br>
</span><span style=3D"color:#660">}</span><span
style=3D"color:#000"><br>
<br>
</span></div>
</code></div>
<br>
<br>
<br>
This can be used as following:<br>
<br>
<div
style=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);bo=
rder-style:solid;border-width:1px;word-wrap:break-word"><code>
<div>
<span style=3D"color:#008">struct</span><span
style=3D"color:#000"> </span><span
style=3D"color:#606">Point</span><span
style=3D"color:#660">{</span><span
style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">int</span><s=
pan
style=3D"color:#000"> x</span><span
style=3D"color:#660">,</span><span
style=3D"color:#000">y</span><span
style=3D"color:#660">;</span><span
style=3D"color:#000"><br>
</span><span style=3D"color:#660">};</span><span
style=3D"color:#000"><br>
<br>
vector</span><span style=3D"color:#660"><</span><s=
pan
style=3D"color:#606">Point</span><span
style=3D"color:#660">></span><span
style=3D"color:#000">vec</span><span
style=3D"color:#660">;</span><span
style=3D"color:#000"><br>
sort</span><span style=3D"color:#660">(</span><span
style=3D"color:#000">vec</span><span
style=3D"color:#660">.</span><span
style=3D"color:#008">begin</span><span
style=3D"color:#660">(),</span><span
style=3D"color:#000"> vec</span><span
style=3D"color:#660">.</span><span
style=3D"color:#008">end</span><span
style=3D"color:#660">(),</span><span
style=3D"color:#000"> order_by</span><span
style=3D"color:#660">([](</span><span
style=3D"color:#606">Point</span><span
style=3D"color:#000"> p</span><span
style=3D"color:#660">){</span><span
style=3D"color:#008">return</span><span
style=3D"color:#000"> p</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">x</span><span
style=3D"color:#660">;}));</span><span
style=3D"color:#000"><br>
</span></div>
</code></div>
<br>
<br>
<br>
Up to now order_by is only a neat addition but nothing
ground breaking. It really shines in combination with
tuple:<br>
<br>
<div
style=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);bo=
rder-style:solid;border-width:1px;word-wrap:break-word"><code>
<div><span style=3D"color:#008">struct</span><span
style=3D"color:#000"> S</span><span
style=3D"color:#660">{</span><span
style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">int</span><s=
pan
style=3D"color:#000"> x</span><span
style=3D"color:#660">,</span><span
style=3D"color:#000">y</span><span
style=3D"color:#660">,</span><span
style=3D"color:#000">z</span><span
style=3D"color:#660">,</span><span
style=3D"color:#000">p</span><span
style=3D"color:#660">,</span><span
style=3D"color:#000">q</span><span
style=3D"color:#660">,</span><span
style=3D"color:#000">r</span><span
style=3D"color:#660">;</span><span
style=3D"color:#000"><br>
</span><span style=3D"color:#660">};</span><span
style=3D"color:#000"><br>
<br>
vector</span><span style=3D"color:#660"><</span><s=
pan
style=3D"color:#000">S</span><span
style=3D"color:#660">></span><span
style=3D"color:#000">vec</span><span
style=3D"color:#660">;</span><span
style=3D"color:#000"><br>
sort</span><span style=3D"color:#660">(</span><span
style=3D"color:#000">vec</span><span
style=3D"color:#660">.</span><span
style=3D"color:#008">begin</span><span
style=3D"color:#660">(),</span><span
style=3D"color:#000"> vec</span><span
style=3D"color:#660">.</span><span
style=3D"color:#008">end</span><span
style=3D"color:#660">(),</span><span
style=3D"color:#000"> order_by</span><span
style=3D"color:#660">([](</span><span
style=3D"color:#000">S p</span><span
style=3D"color:#660">){</span><span
style=3D"color:#008">return</span><span
style=3D"color:#000"> make_tuple</span><span
style=3D"color:#660">(</span><span
style=3D"color:#000">p</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">x</span><span
style=3D"color:#660">,</span><span
style=3D"color:#000"> p</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">p</span><span
style=3D"color:#660">,</span><span
style=3D"color:#000"> p</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">q</span><span
style=3D"color:#660">,</span><span
style=3D"color:#000"> p</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">y</span><span
style=3D"color:#660">);}));</span><span
style=3D"color:#000"><br>
</span></div>
</code></div>
<br>
<br>
<br>
The meaning here is: First sort by x, then by p, then by q
and finally by y. The rest are can come in any order.
Without order_by you would need something like:<br>
<br>
<div
style=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);bo=
rder-style:solid;border-width:1px;word-wrap:break-word"><code>
<div><span style=3D"color:#660">[](</span><span
style=3D"color:#000">S l</span><span
style=3D"color:#660">,</span><span
style=3D"color:#000"> S r</span><span
style=3D"color:#660">){</span><span
style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">if</span><sp=
an
style=3D"color:#660">(</span><span
style=3D"color:#000">l</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">x </span><span
style=3D"color:#660">!=3D</span><span
style=3D"color:#000"> r</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">x</span><span
style=3D"color:#660">)</span><span
style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">retur=
n</span><span
style=3D"color:#000"> l</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">x </span><span
style=3D"color:#660"><</span><span
style=3D"color:#000"> r</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">x</span><span
style=3D"color:#660">;</span><span
style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">if</span><sp=
an
style=3D"color:#660">(</span><span
style=3D"color:#000">l</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">p </span><span
style=3D"color:#660">!=3D</span><span
style=3D"color:#000"> r</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">p</span><span
style=3D"color:#660">)</span><span
style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">retur=
n</span><span
style=3D"color:#000"> l</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">p </span><span
style=3D"color:#660"><</span><span
style=3D"color:#000"> r</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">p</span><span
style=3D"color:#660">;</span><span
style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">if</span><sp=
an
style=3D"color:#660">(</span><span
style=3D"color:#000">l</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">q </span><span
style=3D"color:#660">!=3D</span><span
style=3D"color:#000"> r</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">q</span><span
style=3D"color:#660">)</span><span
style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">retur=
n</span><span
style=3D"color:#000"> l</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">q </span><span
style=3D"color:#660"><</span><span
style=3D"color:#000"> r</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">q</span><span
style=3D"color:#660">;</span><span
style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">if</span><sp=
an
style=3D"color:#660">(</span><span
style=3D"color:#000">l</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">y </span><span
style=3D"color:#660">!=3D</span><span
style=3D"color:#000"> r</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">y</span><span
style=3D"color:#660">)</span><span
style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">retur=
n</span><span
style=3D"color:#000"> l</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">y </span><span
style=3D"color:#660"><</span><span
style=3D"color:#000"> r</span><span
style=3D"color:#660">.</span><span
style=3D"color:#000">y</span><span
style=3D"color:#660">;</span><span
style=3D"color:#000"><br>
=C2=A0 </span><span style=3D"color:#008">return</span=
><span
style=3D"color:#000"> </span><span
style=3D"color:#008">false</span><span
style=3D"color:#660">;</span><span
style=3D"color:#000"><br>
</span><span style=3D"color:#660">}</span><span
style=3D"color:#000"><br>
</span></div>
</code></div>
<br>
<br>
Best Regards,<br>
Ben Strasser<span class=3D"HOEnZb"><font color=3D"#888888"><b=
r>
<br>
</font></span></div>
<span class=3D"HOEnZb"><font color=3D"#888888">
-- <br>
<br>
--- <br>
You received this message because you are subscribed to
the Google Groups "ISO C++ Standard - Future Proposals"
group.<br>
To unsubscribe from this group and stop receiving emails
from it, send an email to <a moz-do-not-send=3D"true"
href=3D"mailto:std-proposals+unsubscribe@isocpp.org"
target=3D"_blank">std-proposals+unsubscribe@isocpp.org</a=
>.<br>
To post to this group, send email to <a
moz-do-not-send=3D"true"
href=3D"mailto:std-proposals@isocpp.org" target=3D"_blank=
">std-proposals@isocpp.org</a>.<br>
Visit this group at <a moz-do-not-send=3D"true"
href=3D"http://groups.google.com/a/isocpp.org/group/std-p=
roposals/"
target=3D"_blank">http://groups.google.com/a/isocpp.org/g=
roup/std-proposals/</a>.<br>
</font></span></blockquote>
</div>
<br>
</div>
-- <br>
<br>
--- <br>
You received this message because you are subscribed to the Google
Groups "ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it,
send an email to <a moz-do-not-send=3D"true"
href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposals+=
unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a moz-do-not-send=3D"true"
href=3D"mailto:std-proposals@isocpp.org">std-proposals@isocpp.org</=
a>.<br>
Visit this group at <a moz-do-not-send=3D"true"
href=3D"http://groups.google.com/a/isocpp.org/group/std-proposals/"=
>http://groups.google.com/a/isocpp.org/group/std-proposals/</a>.<br>
</blockquote>
<br>
</body>
</html>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--------------070509090906090703060604--
.
Author: Jim Porter <jvp4846@g.rit.edu>
Date: Fri, 30 May 2014 10:24:03 -0700 (PDT)
Raw View
------=_Part_473_1083727.1401470643999
Content-Type: text/plain; charset=UTF-8
On Friday, May 30, 2014 12:03:08 PM UTC-5, Andrew Tomazos wrote:
>
> Like instead of:
>
> sort(vec.begin(), vec.end(), std::greater<int>);
>
> I would rather write:
>
> sort(vec.begin(), vec.end(), std::greater);
>
A bit off-topic, but as of C++14, you can write sort(vec.begin(),
vec.end(), std::greater<>);
- Jim
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_473_1083727.1401470643999
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Friday, May 30, 2014 12:03:08 PM UTC-5, Andrew Tomazos =
wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8=
ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><div><d=
iv class=3D"gmail_quote"><div><span style=3D"font-family:monospace;font-siz=
e:10px;background-color:rgb(250,250,250)"><font color=3D"#666600">Like inst=
ead of:</font></span></div>
<div><br></div><div><div><span style=3D"font-family:monospace;font-size:10p=
x;background-color:rgb(250,250,250)"><font color=3D"#666600"> =
sort(vec.begin(), vec.end(), std::greater<int>);</font></span></div><=
/div><div>
<span style=3D"color:rgb(102,102,0);font-family:monospace;font-size:10px;ba=
ckground-color:rgb(250,250,250)"><br></span></div><div><span style=3D"color=
:rgb(102,102,0);font-family:monospace;font-size:10px;background-color:rgb(2=
50,250,250)">I would rather write:</span><br>
</div><div><span style=3D"font-family:monospace;font-size:10px;background-c=
olor:rgb(250,250,250)"><font color=3D"#666600"><br></font></span></div><div=
><span style=3D"font-family:monospace;font-size:10px;background-color:rgb(2=
50,250,250)"><font color=3D"#666600"> sort(vec.begin(), vec.en=
d(), std::greater);</font></span></div></div></div></div></blockquote><div>=
<br>A bit off-topic, but as of C++14, you can write sort(vec.begin(), vec.e=
nd(), std::greater<>);<br><br>- Jim <br></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_473_1083727.1401470643999--
.
Author: Nevin Liber <nevin@eviloverlord.com>
Date: Fri, 30 May 2014 12:25:36 -0500
Raw View
--047d7b3a8176c8f73c04faa157f2
Content-Type: text/plain; charset=UTF-8
On 30 May 2014 12:24, Jim Porter <jvp4846@g.rit.edu> wrote:
> A bit off-topic, but as of C++14, you can write sort(vec.begin(),
> vec.end(), std::greater<>);
>
shouldn't that be
sort(vec.begin(), vec.end(), std::greater<>());
>
> - Jim
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> Visit this group at
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>
--
Nevin ":-)" Liber <mailto:nevin@eviloverlord.com> (847) 691-1404
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--047d7b3a8176c8f73c04faa157f2
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On 30 May 2014 12:24, Jim Porter <span dir=3D"ltr"><<a =
href=3D"mailto:jvp4846@g.rit.edu" target=3D"_blank">jvp4846@g.rit.edu</a>&g=
t;</span> wrote:<br><div class=3D"gmail_extra"><div class=3D"gmail_quote"><=
blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px=
#ccc solid;padding-left:1ex">
<div dir=3D"ltr"><div class=3D"">A bit off-topic, but as of C++14, you can =
write sort(vec.begin(), vec.end(), std::greater<>);<br></div></div></=
blockquote><div><br></div><div>shouldn't that be</div><div><br></div><d=
iv>
sort(vec.begin(), vec.end(), std::greater<>());=C2=A0</div><blockquot=
e class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc sol=
id;padding-left:1ex"><div dir=3D"ltr"><div class=3D""></div><div><br>-=C2=
=A0 Jim <br></div>
</div><div class=3D"HOEnZb"><div class=3D"h5">
<p></p>
-- <br>
<br>
--- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br>
</div></div></blockquote></div><br><br clear=3D"all"><div><br></div>-- <br>=
=C2=A0Nevin ":-)" Liber=C2=A0 <mailto:<a href=3D"mailto:nevin@=
eviloverlord.com" target=3D"_blank">nevin@eviloverlord.com</a>>=C2=A0 (8=
47) 691-1404
</div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--047d7b3a8176c8f73c04faa157f2--
.
Author: "'Geoffrey Romer' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Fri, 30 May 2014 15:22:37 -0700
Raw View
--089e015386a29c251d04faa57b93
Content-Type: text/plain; charset=UTF-8
On Fri, May 30, 2014 at 10:03 AM, Andrew Tomazos <andrewtomazos@gmail.com>
wrote:
> On Fri, May 30, 2014 at 6:29 PM, 'Geoffrey Romer' via ISO C++ Standard -
> Future Proposals <std-proposals@isocpp.org> wrote:
>
>> I generally really like this. I do see a couple problems, though:
>> - There's no obvious way to control the comparison that's applied to the
>> extracted values. For example, what if I want to sort in decreasing order?
>> - It doesn't seem to work very well when you need to name the comparator
>> type, e.g. to use it with an associative container.
>>
>
> To your first point, I was going to suggest that the second parameter to
> order_by be a comparator that defaults to std::less, but the problem is the
> same as the second point, it seems as though you would have to name the
> type-id:
>
> sort(vec.begin(), vec.end(), order_by([](const S& p){return tie(p.x, p
> .p, p.q, p.y)}, std::greater<std::tuple<int&, int&, int&, int&>>);
>
> I'd rather write:
>
> sort(vec.begin(), vec.end(), order_by([](const S& p){return tie(p.x, p
> .p, p.q, p.y)}, std::greater);
>
As others have pointed out, stick <>() on the end, and this will work in
C++14. Even if greater had not gotten this fix, you could still do this:
sort(vec.begin(), vec.end(), order_by([](const S& p){return tie(p.x, p.p, p.
q, p.y)},
[](const auto& l, const auto&
r){return l < r;});
So you don't have to name the type. What troubles me about this approach is
that the code's not very transparent to the reader; it's not obvious how
the two arguments relate to each other, or what they mean as a whole,
without reference to the order_by documentation. I think part of what
bothers me is the name: it's much more specific than its name implies.
Something like project_comparator or transform_comparator might be better.
>
>
> Like instead of:
>
> sort(vec.begin(), vec.end(), std::greater<int>);
>
> I would rather write:
>
> sort(vec.begin(), vec.end(), std::greater);
>
> (actually I would rather write:
>
> sort(vec, std::greater);
>
> but that's a different story.)
>
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> Visit this group at
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--089e015386a29c251d04faa57b93
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">=
On Fri, May 30, 2014 at 10:03 AM, Andrew Tomazos <span dir=3D"ltr"><<a h=
ref=3D"mailto:andrewtomazos@gmail.com" target=3D"_blank">andrewtomazos@gmai=
l.com</a>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-=
left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;p=
adding-left:1ex"><div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"=
gmail_quote">
<div class=3D"">On Fri, May 30, 2014 at 6:29 PM, 'Geoffrey Romer' v=
ia ISO C++ Standard - Future Proposals <span dir=3D"ltr"><<a href=3D"mai=
lto:std-proposals@isocpp.org" target=3D"_blank">std-proposals@isocpp.org</a=
>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-=
left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;p=
adding-left:1ex"><div dir=3D"ltr">I generally really like this. I do see a =
couple problems, though:<div>
- There's no obvious way to control the comparison that's applied t=
o the extracted values. For example, what if I want to sort in decreasing o=
rder?<br>
</div><div>- It doesn't seem to work very well when you need to name th=
e comparator type, e.g. to use it with an associative container.</div></div=
></blockquote><div><br></div></div><div>To your first point, I was going to=
suggest that the second parameter to order_by be a comparator that default=
s to std::less, but the problem is the same as the second point, it seems a=
s though you would have to name the type-id:</div>
<div><br></div><div><span style=3D"font-family:monospace;font-size:10px;col=
or:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0 =C2=A0 sort</span><=
span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);bac=
kground-color:rgb(250,250,250)">(</span><span style=3D"font-family:monospac=
e;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">vec</s=
pan><span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0=
);background-color:rgb(250,250,250)">.</span><span style=3D"font-family:mon=
ospace;font-size:10px;color:rgb(0,0,136);background-color:rgb(250,250,250)"=
>begin</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(=
102,102,0);background-color:rgb(250,250,250)">(),</span><span style=3D"font=
-family:monospace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,=
250,250)">=C2=A0vec</span><span style=3D"font-family:monospace;font-size:10=
px;color:rgb(102,102,0);background-color:rgb(250,250,250)">.</span><span st=
yle=3D"font-family:monospace;font-size:10px;color:rgb(0,0,136);background-c=
olor:rgb(250,250,250)">end</span><span style=3D"font-family:monospace;font-=
size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">(),</span=
><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);backg=
round-color:rgb(250,250,250)">=C2=A0order_by</span><span style=3D"font-fami=
ly:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(250,2=
50,250)">([](const=C2=A0</span><span style=3D"font-family:monospace;font-si=
ze:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">S& p</span>=
<span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);ba=
ckground-color:rgb(250,250,250)">){</span><span style=3D"font-family:monosp=
ace;font-size:10px;color:rgb(0,0,136);background-color:rgb(250,250,250)">re=
turn</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,=
0,0);background-color:rgb(250,250,250)">=C2=A0tie</span><span style=3D"font=
-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(=
250,250,250)">(</span><span style=3D"font-family:monospace;font-size:10px;c=
olor:rgb(0,0,0);background-color:rgb(250,250,250)">p</span><span style=3D"f=
ont-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:r=
gb(250,250,250)">.</span><span style=3D"font-family:monospace;font-size:10p=
x;color:rgb(0,0,0);background-color:rgb(250,250,250)">x</span><span style=
=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);background-co=
lor:rgb(250,250,250)">,</span><span style=3D"font-family:monospace;font-siz=
e:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0p</span><s=
pan style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);back=
ground-color:rgb(250,250,250)">.</span><span style=3D"font-family:monospace=
;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">p</span=
><span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);b=
ackground-color:rgb(250,250,250)">,</span><span style=3D"font-family:monosp=
ace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=
=A0p</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(10=
2,102,0);background-color:rgb(250,250,250)">.</span><span style=3D"font-fam=
ily:monospace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,=
250)">q</span><span style=3D"font-family:monospace;font-size:10px;color:rgb=
(102,102,0);background-color:rgb(250,250,250)">,</span><span style=3D"font-=
family:monospace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,2=
50,250)">=C2=A0p</span><span style=3D"font-family:monospace;font-size:10px;=
color:rgb(102,102,0);background-color:rgb(250,250,250)">.</span><span style=
=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);background-color:=
rgb(250,250,250)">y</span><span style=3D"font-family:monospace;font-size:10=
px;background-color:rgb(250,250,250)"><font color=3D"#666600">)}, std::grea=
ter<std::tuple<int&, int&, int&, int&>>);</font=
></span><br>
</div><div><span style=3D"font-family:monospace;font-size:10px;background-c=
olor:rgb(250,250,250)"><font color=3D"#666600"><br></font></span></div><div=
><span style=3D"font-family:monospace;font-size:10px;background-color:rgb(2=
50,250,250)"><font color=3D"#666600">I'd rather write:</font></span></d=
iv>
<div><span style=3D"font-family:monospace;font-size:10px;background-color:r=
gb(250,250,250)"><font color=3D"#666600"><br></font></span></div><div><div>=
<span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);backgr=
ound-color:rgb(250,250,250)">=C2=A0 =C2=A0 sort</span><span style=3D"font-f=
amily:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(25=
0,250,250)">(</span><span style=3D"font-family:monospace;font-size:10px;col=
or:rgb(0,0,0);background-color:rgb(250,250,250)">vec</span><span style=3D"f=
ont-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:r=
gb(250,250,250)">.</span><span style=3D"font-family:monospace;font-size:10p=
x;color:rgb(0,0,136);background-color:rgb(250,250,250)">begin</span><span s=
tyle=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);backgroun=
d-color:rgb(250,250,250)">(),</span><span style=3D"font-family:monospace;fo=
nt-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0vec<=
/span><span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102=
,0);background-color:rgb(250,250,250)">.</span><span style=3D"font-family:m=
onospace;font-size:10px;color:rgb(0,0,136);background-color:rgb(250,250,250=
)">end</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(=
102,102,0);background-color:rgb(250,250,250)">(),</span><span style=3D"font=
-family:monospace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,=
250,250)">=C2=A0order_by</span><span style=3D"font-family:monospace;font-si=
ze:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">([](const=
=C2=A0</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(=
0,0,0);background-color:rgb(250,250,250)">S& p</span><span style=3D"fon=
t-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb=
(250,250,250)">){</span><span style=3D"font-family:monospace;font-size:10px=
;color:rgb(0,0,136);background-color:rgb(250,250,250)">return</span><span s=
tyle=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);background-co=
lor:rgb(250,250,250)">=C2=A0tie</span><span style=3D"font-family:monospace;=
font-size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">(</s=
pan><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);ba=
ckground-color:rgb(250,250,250)">p</span><span style=3D"font-family:monospa=
ce;font-size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">.=
</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0)=
;background-color:rgb(250,250,250)">x</span><span style=3D"font-family:mono=
space;font-size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)=
">,</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0=
,0);background-color:rgb(250,250,250)">=C2=A0p</span><span style=3D"font-fa=
mily:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(250=
,250,250)">.</span><span style=3D"font-family:monospace;font-size:10px;colo=
r:rgb(0,0,0);background-color:rgb(250,250,250)">p</span><span style=3D"font=
-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(=
250,250,250)">,</span><span style=3D"font-family:monospace;font-size:10px;c=
olor:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0p</span><span styl=
e=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);background-c=
olor:rgb(250,250,250)">.</span><span style=3D"font-family:monospace;font-si=
ze:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">q</span><span s=
tyle=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);backgroun=
d-color:rgb(250,250,250)">,</span><span style=3D"font-family:monospace;font=
-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0p</spa=
n><span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);=
background-color:rgb(250,250,250)">.</span><span style=3D"font-family:monos=
pace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">y</=
span><span style=3D"font-family:monospace;font-size:10px;background-color:r=
gb(250,250,250)"><font color=3D"#666600">)}, std::greater);</font></span></=
div>
</div></div></div></div></blockquote><div><br></div><div>As others have poi=
nted out, stick <>() on the end, and this will work in C++14. Even if=
greater had not gotten this fix, you could still do this:</div><div><br>
</div><div><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,=
0,0);background-color:rgb(250,250,250)">sort</span><span style=3D"font-fami=
ly:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(250,2=
50,250)">(</span><span style=3D"font-family:monospace;font-size:10px;color:=
rgb(0,0,0);background-color:rgb(250,250,250)">vec</span><span style=3D"font=
-family:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(=
250,250,250)">.</span><span style=3D"font-family:monospace;font-size:10px;c=
olor:rgb(0,0,136);background-color:rgb(250,250,250)">begin</span><span styl=
e=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);background-c=
olor:rgb(250,250,250)">(),</span><span style=3D"font-family:monospace;font-=
size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0vec</sp=
an><span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0)=
;background-color:rgb(250,250,250)">.</span><span style=3D"font-family:mono=
space;font-size:10px;color:rgb(0,0,136);background-color:rgb(250,250,250)">=
end</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(102=
,102,0);background-color:rgb(250,250,250)">(),</span><span style=3D"font-fa=
mily:monospace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250=
,250)">=C2=A0order_by</span><span style=3D"font-family:monospace;font-size:=
10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">([](const=C2=
=A0</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0=
,0);background-color:rgb(250,250,250)">S& p</span><span style=3D"font-f=
amily:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(25=
0,250,250)">){</span><span style=3D"font-family:monospace;font-size:10px;co=
lor:rgb(0,0,136);background-color:rgb(250,250,250)">return</span><span styl=
e=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);background-color=
:rgb(250,250,250)">=C2=A0tie</span><span style=3D"font-family:monospace;fon=
t-size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">(</span=
><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);backg=
round-color:rgb(250,250,250)">p</span><span style=3D"font-family:monospace;=
font-size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">.</s=
pan><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0);ba=
ckground-color:rgb(250,250,250)">x</span><span style=3D"font-family:monospa=
ce;font-size:10px;color:rgb(102,102,0);background-color:rgb(250,250,250)">,=
</span><span style=3D"font-family:monospace;font-size:10px;color:rgb(0,0,0)=
;background-color:rgb(250,250,250)">=C2=A0p</span><span style=3D"font-famil=
y:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(250,25=
0,250)">.</span><span style=3D"font-family:monospace;font-size:10px;color:r=
gb(0,0,0);background-color:rgb(250,250,250)">p</span><span style=3D"font-fa=
mily:monospace;font-size:10px;color:rgb(102,102,0);background-color:rgb(250=
,250,250)">,</span><span style=3D"font-family:monospace;font-size:10px;colo=
r:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0p</span><span style=
=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);background-co=
lor:rgb(250,250,250)">.</span><span style=3D"font-family:monospace;font-siz=
e:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">q</span><span st=
yle=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);background=
-color:rgb(250,250,250)">,</span><span style=3D"font-family:monospace;font-=
size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">=C2=A0p</span=
><span style=3D"font-family:monospace;font-size:10px;color:rgb(102,102,0);b=
ackground-color:rgb(250,250,250)">.</span><span style=3D"font-family:monosp=
ace;font-size:10px;color:rgb(0,0,0);background-color:rgb(250,250,250)">y</s=
pan><span style=3D"font-family:monospace;font-size:10px;background-color:rg=
b(250,250,250)"><font color=3D"#666600">)},</font></span></div>
<div><span style=3D"font-family:monospace;font-size:10px;background-color:r=
gb(250,250,250)"><font color=3D"#666600">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 [](const auto& l, const auto& r){return l =
< r;});</font></span><br>
</div><div><span style=3D"font-family:monospace;font-size:10px;background-c=
olor:rgb(250,250,250)"><font color=3D"#666600"><br></font></span></div>So y=
ou don't have to name the type. What troubles me about this approach is=
that the code's not very transparent to the reader; it's not obvio=
us how the two arguments relate to each other, or what they mean as a whole=
, without reference to the order_by documentation. I think part of what bot=
hers me is the name: it's much more specific than its name implies. Som=
ething like project_comparator or transform_comparator might be better.</di=
v>
<div class=3D"gmail_quote"><div>=C2=A0</div><blockquote class=3D"gmail_quot=
e" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-colo=
r:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir=3D"lt=
r"><div class=3D"gmail_extra">
<div class=3D"gmail_quote"><div><div><br>
</div></div><div><span style=3D"font-family:monospace;font-size:10px;backgr=
ound-color:rgb(250,250,250)"><font color=3D"#666600"><br></font></span></di=
v><div><span style=3D"font-family:monospace;font-size:10px;background-color=
:rgb(250,250,250)"><font color=3D"#666600">Like instead of:</font></span></=
div>
<div><br></div><div><div><span style=3D"font-family:monospace;font-size:10p=
x;background-color:rgb(250,250,250)"><font color=3D"#666600">=C2=A0 =C2=A0 =
sort(vec.begin(), vec.end(), std::greater<int>);</font></span></div><=
/div><div>
<span style=3D"color:rgb(102,102,0);font-family:monospace;font-size:10px;ba=
ckground-color:rgb(250,250,250)"><br></span></div><div><span style=3D"color=
:rgb(102,102,0);font-family:monospace;font-size:10px;background-color:rgb(2=
50,250,250)">I would rather write:</span><br>
</div><div><span style=3D"font-family:monospace;font-size:10px;background-c=
olor:rgb(250,250,250)"><font color=3D"#666600"><br></font></span></div><div=
><span style=3D"font-family:monospace;font-size:10px;background-color:rgb(2=
50,250,250)"><font color=3D"#666600">=C2=A0 =C2=A0 sort(vec.begin(), vec.en=
d(), std::greater);</font></span></div>
<div><br></div><div><span style=3D"font-family:monospace;font-size:10px;bac=
kground-color:rgb(250,250,250)"><font color=3D"#666600">(actually I would r=
ather write:</font></span></div><div><span style=3D"font-family:monospace;f=
ont-size:10px;background-color:rgb(250,250,250)"><font color=3D"#666600"><b=
r>
</font></span></div><div><span style=3D"font-family:monospace;font-size:10p=
x;background-color:rgb(250,250,250)"><font color=3D"#666600">=C2=A0 =C2=A0 =
sort(vec, std::greater);</font></span></div><div><span style=3D"font-family=
:monospace;font-size:10px;background-color:rgb(250,250,250)"><font color=3D=
"#666600"><br>
</font></span></div><div><span style=3D"font-family:monospace;font-size:10p=
x;background-color:rgb(250,250,250)"><font color=3D"#666600">but that's=
a different story.)</font></span></div><div><span style=3D"font-family:mon=
ospace;font-size:10px;background-color:rgb(250,250,250)"><font color=3D"#66=
6600"><br>
</font></span></div><div><span style=3D"font-family:monospace;font-size:10p=
x;background-color:rgb(250,250,250)"><font color=3D"#666600"><br></font></s=
pan></div></div></div></div><div class=3D""><div class=3D"h5">
<p></p>
-- <br>
<br>
--- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br>
</div></div></blockquote></div><br></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--089e015386a29c251d04faa57b93--
.
Author: Nevin Liber <nevin@eviloverlord.com>
Date: Fri, 30 May 2014 17:32:50 -0500
Raw View
--047d7b3a81768a3dea04faa5a2a3
Content-Type: text/plain; charset=UTF-8
On 30 May 2014 12:03, Andrew Tomazos <andrewtomazos@gmail.com> wrote:
> To your first point, I was going to suggest that the second parameter to
> order_by be a comparator that defaults to std::less, but the problem is the
> same as the second point,
>
Here is a C++1y implementation of such a beast (note: I just whipped this
up, and might have gotten some of the corner cases incorrect; feel free to
fix it):
// Deriving from tuple for EBO
template<typename Identity, typename Compare>
class OrderBy : std::tuple<Identity, Compare>
{
using tuple_type = std::tuple<Identity, Compare>;
decltype(auto) asTuple() const noexcept
{ return static_cast<tuple_type const&>(*this); }
decltype(auto) getIdentity() const noexcept
{ return std::get<0>(asTuple()); }
decltype(auto) getCompare() const noexcept
{ return std::get<1>(asTuple()); }
public:
using identity_type = Identity;
using compare_type = Compare;
// Need the enable_if so copy constructor from non-const l-value
reference works
template<typename I, typename =
std::enable_if_t<!std::is_convertible<I, OrderBy>()>>
explicit OrderBy(I&& i)
: tuple_type{std::forward<I>(i), Compare{}}
{}
template<typename I, typename C>
explicit OrderBy(I&& i, C&& c)
: tuple_type{std::forward<I>(i), std::forward<C>(c)}
{}
template<typename L, typename R>
decltype(auto) operator()(L&& l, R&& r) const
{ return getCompare()(getIdentity()(std::forward<L>(l)),
getIdentity()(std::forward<R>(r))); }
};
// Defaults to using std::less<>
template<typename Identity>
auto order_by(Identity&& i)
{ return OrderBy<std::remove_reference_t<Identity>,
std::less<>>(std::forward<Identity>(i)); }
// Explicitly specify the comparator type, as in order_by<std::greater<>>(f)
template<typename Compare, typename Identity>
auto order_by(Identity&& i)
{ return OrderBy<std::remove_reference_t<Identity>,
Compare>(std::forward<Identity>(i)); }
// Deduce the comparator type from the second parameter
// template parameters are in reverse order from previous function
// to prevent accidently deducing and specifying comparator type
template<typename Identity, typename Compare>
auto order_by(Identity&& i, Compare&& c)
{ return OrderBy<std::remove_reference_t<Identity>,
std::remove_reference_t<Compare>>(std::forward<Identity>(i),
std::forward<Compare>(c)); }
It's certainly an interesting idea.
--
Nevin ":-)" Liber <mailto:nevin@eviloverlord.com> (847) 691-1404
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--047d7b3a81768a3dea04faa5a2a3
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On 30 May 2014 12:03, Andrew Tomazos <span dir=3D"ltr"><=
;<a href=3D"mailto:andrewtomazos@gmail.com" target=3D"_blank">andrewtomazos=
@gmail.com</a>></span> wrote:<br><div class=3D"gmail_extra"><div class=
=3D"gmail_quote">
<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-=
left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;p=
adding-left:1ex"><div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"=
gmail_quote">
<div class=3D"">To your first point, I was going to suggest that the second=
parameter to order_by be a comparator that defaults to std::less, but the =
problem is the same as the second point, </div></div></div></div></blockquo=
te>
<div><br></div><div>Here is a C++1y implementation of such a beast (note: =
=C2=A0I just whipped this up, and might have gotten some of the corner case=
s incorrect; feel free to fix it):</div><div><br></div><div><p style=3D"mar=
gin:0px;font-size:11px;font-family:Menlo;color:rgb(0,132,0)">
// Deriving from tuple for EBO</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,16=
2)">template<span style=3D"color:rgb(0,0,0)"><</span>typename<span style=
=3D"color:rgb(0,0,0)"> Identity, </span>typename<span style=3D"color:rgb(0,=
0,0)"> Compare></span></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo"><span style=3D"col=
or:rgb(187,44,162)">class</span> OrderBy : std::tuple<Identity, Compare&=
gt;</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">{</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">=C2=A0 =C2=A0 <spa=
n style=3D"color:rgb(187,44,162)">using</span> tuple_type =3D std::tuple<=
;Identity, Compare>;</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><b=
r></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,16=
2)"><span style=3D"color:rgb(0,0,0)">=C2=A0 =C2=A0 </span>decltype<span sty=
le=3D"color:rgb(0,0,0)">(</span>auto<span style=3D"color:rgb(0,0,0)">) asTu=
ple() </span>const<span style=3D"color:rgb(0,0,0)"> </span>noexcept</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">=C2=A0 =C2=A0 { <s=
pan style=3D"color:rgb(187,44,162)">return</span> <span style=3D"color:rgb(=
187,44,162)">static_cast</span><tuple_type <span style=3D"color:rgb(187,=
44,162)">const</span>&>(*<span style=3D"color:rgb(187,44,162)">this<=
/span>); }</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><b=
r></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,16=
2)"><span style=3D"color:rgb(0,0,0)">=C2=A0 =C2=A0 </span>decltype<span sty=
le=3D"color:rgb(0,0,0)">(</span>auto<span style=3D"color:rgb(0,0,0)">) getI=
dentity() </span>const<span style=3D"color:rgb(0,0,0)"> </span>noexcept</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">=C2=A0 =C2=A0 { <s=
pan style=3D"color:rgb(187,44,162)">return</span> std::get<<span style=
=3D"color:rgb(39,42,216)">0</span>>(asTuple()); }</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><b=
r></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,16=
2)"><span style=3D"color:rgb(0,0,0)">=C2=A0 =C2=A0 </span>decltype<span sty=
le=3D"color:rgb(0,0,0)">(</span>auto<span style=3D"color:rgb(0,0,0)">) getC=
ompare() </span>const<span style=3D"color:rgb(0,0,0)"> </span>noexcept</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">=C2=A0 =C2=A0 { <s=
pan style=3D"color:rgb(187,44,162)">return</span> std::get<<span style=
=3D"color:rgb(39,42,216)">1</span>>(asTuple()); }</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;min-height:13px">=
=C2=A0=C2=A0 =C2=A0</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,16=
2)">public<span style=3D"color:rgb(0,0,0)">:</span></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">=C2=A0 =C2=A0 <spa=
n style=3D"color:rgb(187,44,162)">using</span> identity_type =3D Identity;<=
/p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">=C2=A0 =C2=A0 <spa=
n style=3D"color:rgb(187,44,162)">using</span> compare_type =3D Compare;</p=
>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><b=
r></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,132,0)"=
><span style=3D"color:rgb(0,0,0)">=C2=A0 =C2=A0 </span>// Need the enable_i=
f so copy constructor from non-const l-value reference works</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">=C2=A0 =C2=A0 <spa=
n style=3D"color:rgb(187,44,162)">template</span><<span style=3D"color:r=
gb(187,44,162)">typename</span> I, <span style=3D"color:rgb(187,44,162)">ty=
pename</span> =3D std::enable_if_t<!std::is_convertible<I, OrderBy>=
;()>></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">=C2=A0 =C2=A0 <spa=
n style=3D"color:rgb(187,44,162)">explicit</span> OrderBy(I&& i)</p=
>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">=C2=A0 =C2=A0 : tu=
ple_type{std::forward<I>(i), Compare{}}</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">=C2=A0 =C2=A0 {}</=
p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><b=
r></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,16=
2)"><span style=3D"color:rgb(0,0,0)">=C2=A0 =C2=A0 </span>template<span sty=
le=3D"color:rgb(0,0,0)"><</span>typename<span style=3D"color:rgb(0,0,0)"=
> I, </span>typename<span style=3D"color:rgb(0,0,0)"> C></span></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">=C2=A0 =C2=A0 <spa=
n style=3D"color:rgb(187,44,162)">explicit</span> OrderBy(I&& i, C&=
amp;& c)</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">=C2=A0 =C2=A0 : tu=
ple_type{std::forward<I>(i), std::forward<C>(c)}</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">=C2=A0 =C2=A0 {}</=
p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><b=
r></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,16=
2)"><span style=3D"color:rgb(0,0,0)">=C2=A0 =C2=A0 </span>template<span sty=
le=3D"color:rgb(0,0,0)"><</span>typename<span style=3D"color:rgb(0,0,0)"=
> L, </span>typename<span style=3D"color:rgb(0,0,0)"> R></span></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,16=
2)"><span style=3D"color:rgb(0,0,0)">=C2=A0 =C2=A0 </span>decltype<span sty=
le=3D"color:rgb(0,0,0)">(</span>auto<span style=3D"color:rgb(0,0,0)">) </sp=
an>operator<span style=3D"color:rgb(0,0,0)">()(L&& l, R&& r=
) </span>const</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">=C2=A0 =C2=A0 { <s=
pan style=3D"color:rgb(187,44,162)">return</span> getCompare()(getIdentity(=
)(std::forward<L>(l)), getIdentity()(std::forward<R>(r))); }</p=
>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">};</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><b=
r></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,132,0)"=
>// Defaults to using std::less<></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,16=
2)">template<span style=3D"color:rgb(0,0,0)"><</span>typename<span style=
=3D"color:rgb(0,0,0)"> Identity></span></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo"><span style=3D"col=
or:rgb(187,44,162)">auto</span> order_by(Identity&& i)</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">{ <span style=3D"c=
olor:rgb(187,44,162)">return</span> OrderBy<std::remove_reference_t<I=
dentity>, std::less<>>(std::forward<Identity>(i)); }</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><b=
r></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,132,0)"=
>// Explicitly specify the comparator type, as in order_by<std::greater&=
lt;>>(f)</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,16=
2)">template<span style=3D"color:rgb(0,0,0)"><</span>typename<span style=
=3D"color:rgb(0,0,0)"> Compare, </span>typename<span style=3D"color:rgb(0,0=
,0)"> Identity></span></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo"><span style=3D"col=
or:rgb(187,44,162)">auto</span> order_by(Identity&& i)</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">{ <span style=3D"c=
olor:rgb(187,44,162)">return</span> OrderBy<std::remove_reference_t<I=
dentity>, Compare>(std::forward<Identity>(i)); }</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><b=
r></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,132,0)"=
>// Deduce the comparator type from the second parameter</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,132,0)"=
>//=C2=A0 template parameters are in reverse order from previous function</=
p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,132,0)"=
>//=C2=A0 to prevent accidently deducing and specifying comparator type</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,16=
2)">template<span style=3D"color:rgb(0,0,0)"><</span>typename<span style=
=3D"color:rgb(0,0,0)"> Identity, </span>typename<span style=3D"color:rgb(0,=
0,0)"> Compare></span></p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo"><span style=3D"col=
or:rgb(187,44,162)">auto</span> order_by(Identity&& i, Compare&=
& c)</p>
<p style=3D"margin:0px;font-size:11px;font-family:Menlo">{ <span style=3D"c=
olor:rgb(187,44,162)">return</span> OrderBy<std::remove_reference_t<I=
dentity>, std::remove_reference_t<Compare>>(std::forward<Ide=
ntity>(i), std::forward<Compare>(c)); }</p>
</div><div><br></div><div>It's certainly an interesting idea.</div><div=
>--=C2=A0<br></div></div>=C2=A0Nevin ":-)" Liber=C2=A0 <mailto=
:<a href=3D"mailto:nevin@eviloverlord.com" target=3D"_blank">nevin@evilover=
lord.com</a>>=C2=A0 (847) 691-1404
</div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--047d7b3a81768a3dea04faa5a2a3--
.
Author: strasser.ben@gmail.com
Date: Sat, 31 May 2014 00:56:05 -0700 (PDT)
Raw View
------=_Part_1150_29018663.1401522965781
Content-Type: text/plain; charset=UTF-8
Hi,
To your first point, I was going to suggest that the second parameter to
> order_by be a comparator that defaults to std::less, but the problem is the
> same as the second point, it seems as though you would have to name the
> type-id:
>
I do not like this for three reasons:
1) I so no benefit over adding a generally useful reverse_order function
that could be used like this:
sort(vec.begin(), vec.end(), reverse_order(order_by([](Point p){return p.x
;})));
The benefit of reverse_order is that it works with every comparator as
input (i.e. more general) and it is clear at the call site without the
documentation what is going on.
2) You do not really solve the problem. You can not encode the requirement:
"Sort first by x increasing and then by y decreasing". You can only encode
"Sort first by x then by y both increasing" or "Sort first by x then by y
both decreasing".
3) This is a pure convenience feature. There is nothing that can not be
done without it. There is therefore no need to work in every imaginable
border case. I strongly advocate keeping the syntax simple, even if this
means that it is only usable in 90% of the cases. With simple syntax I mean
among other things omitting parameters that are only used in 10% of the
cases. IMO in 90% of the use cases things are ordered by int, floats and
strings, either increasingly or decreasingly. For floats already the
initial proposition is good enough:
sort(vec.begin(), vec.end(), order_by([](Point p){return make_tuple(p.x, -p.
y);}));
This nearly also works for ints. I write nearly because of the problem that
-numeric_limits<int>::min() == 0 leads to subtle bugs. We can however do
the following for ints:
sort(vec.begin(), vec.end(), order_by([](Point p){return make_tuple(p.x, ~p.
y);}));
This works but IMO it is way to easy to make a mistake here. For strings I
unfortunately do not see such an easy method.
Perhaps adding a decreasing function would be useful:
int decreasing(int x){return ~x;}
float decreasing(float x){return -x;}
string decreasing(string x){for(auto&y:x)x=~x;return move(x);}
and then use it as following:
sort(vec.begin(), vec.end(), order_by([](Point p){return make_tuple(p.x,
decreasing(p.y));}));
For the record: Another neat usecase (that I consider in the 10%) that
works out of the box is ordering points in the plane in circular order
around the origin:
sort(vec.begin(), vec.end(), order_by([](Point p){return atan2(p.y, p.x
);}));
Best Regards
Ben Strasser
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_1150_29018663.1401522965781
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hi,<br><br><blockquote class=3D"gmail_quote" style=3D"marg=
in: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><d=
iv dir=3D"ltr"><div><div class=3D"gmail_quote"><div>To your first point, I =
was going to suggest that the second parameter to order_by be a comparator =
that defaults to std::less, but the problem is the same as the second point=
, it seems as though you would have to name the type-id:</div></div></div><=
/div></blockquote><div><br>I do not like this for three reasons:<br><br>1) =
I so no benefit over adding a generally useful reverse_order function that =
could be used like this:<br><br><div class=3D"prettyprint" style=3D"backgro=
und-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-sty=
le: solid; border-width: 1px; word-wrap: break-word;"><code class=3D"pretty=
print"><div class=3D"subprettyprint"><span style=3D"color: #000;" class=3D"=
styled-by-prettify">sort</span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify">vec</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.<=
/span><span style=3D"color: #008;" class=3D"styled-by-prettify">begin</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">(),</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> vec</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">end</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">(),</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> reverse_order</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify">order_by</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">([](</span><span style=3D"color: #606;" class=3D"styled-by=
-prettify">Point</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> p</span><span style=3D"color: #660;" class=3D"styled-by-prettify">){=
</span><span style=3D"color: #008;" class=3D"styled-by-prettify">return</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> p</span><span=
style=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify">x</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">;})));</span></div></code></div><br>The =
benefit of reverse_order is that it works with every comparator as input (i=
..e. more general) and it is clear at the call site without the documentatio=
n what is going on.<br> <br>2) You do not really solve the problem. Yo=
u can not encode the requirement: "Sort first by x increasing and then by y=
decreasing". You can only encode "Sort first by x then by y both increasin=
g" or "Sort first by x then by y both decreasing".<br><br>3) This is a pure=
convenience feature. There is nothing that can not be done without it. The=
re is therefore no need to work in every imaginable border case. I strongly=
advocate keeping the syntax simple, even if this means that it is only usa=
ble in 90% of the cases. With simple syntax I mean among other things omitt=
ing parameters that are only used in 10% of the cases. IMO in 90% of the us=
e cases things are ordered by int, floats and strings, either increasingly =
or decreasingly. For floats already the initial proposition is good enough:=
<br><br><div class=3D"prettyprint" style=3D"background-color: rgb(250, 250,=
250); border-color: rgb(187, 187, 187); border-style: solid; border-width:=
1px; word-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"sub=
prettyprint"><span style=3D"color: #000;" class=3D"styled-by-prettify">sort=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify">vec</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"col=
or: #008;" class=3D"styled-by-prettify">begin</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">(),</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> vec</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">.</span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">end</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">(),</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> order_by</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
([](</span><span style=3D"color: #606;" class=3D"styled-by-prettify">Point<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> p</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">){</span><span styl=
e=3D"color: #008;" class=3D"styled-by-prettify">return</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify"> make_tuple</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">p</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify">x</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">-</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify">p</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify">y</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">);}));</span></div></code></div><br>This ne=
arly also works for ints. I write nearly because of the problem that -numer=
ic_limits<int>::min() =3D=3D 0 leads to subtle bugs. We can however d=
o the following for ints:<br><br><div class=3D"prettyprint" style=3D"backgr=
ound-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-st=
yle: solid; border-width: 1px; word-wrap: break-word;"><code class=3D"prett=
yprint"><div class=3D"subprettyprint"><span style=3D"color: #000;" class=3D=
"styled-by-prettify">sort</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify">vec</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.=
</span><span style=3D"color: #008;" class=3D"styled-by-prettify">begin</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">(),</span><span=
style=3D"color: #000;" class=3D"styled-by-prettify"> vec</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">end</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">(),</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> order_by</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">([](</span><span style=3D"color: #606;" class=3D"st=
yled-by-prettify">Point</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> p</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">){</span><span style=3D"color: #008;" class=3D"styled-by-prettify">ret=
urn</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> make_t=
uple</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify">p</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify">x</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">~</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify">p</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify">y</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">);}));</span></div></=
code></div><br>This works but IMO it is way to easy to make a mistake here.=
For strings I unfortunately do not see such an easy method.<br><br>Perhaps=
adding a decreasing function would be useful:<br><br><div class=3D"prettyp=
rint" style=3D"background-color: rgb(250, 250, 250); border-color: rgb(187,=
187, 187); border-style: solid; border-width: 1px; word-wrap: break-word;"=
><code class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">int</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> decreasing</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">int</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> x</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">){</span><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">return</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">~</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify">x</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">;}</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">float</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> decreasing</span><span style=3D"color: #660;"=
class=3D"styled-by-prettify">(</span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">float</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> x</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">){</span><span style=3D"color: #008;" class=3D"styled-by-prettify">=
return</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">-</span><span=
style=3D"color: #000;" class=3D"styled-by-prettify">x</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">;}</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #008;"=
class=3D"styled-by-prettify">string</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> decreasing</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">(</span><span style=3D"color: #008;" class=3D"st=
yled-by-prettify">string</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> x</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">){</span><span style=3D"color: #008;" class=3D"styled-by-prettify">fo=
r</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><=
span style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">&</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">y</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">:</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify">x</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify">x</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">=3D~</span><span style=3D"color: #000;" class=3D"styled-by-prettify">x</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span=
style=3D"color: #008;" class=3D"styled-by-prettify">return</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> move</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify">x</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">);}</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"><br></span></div></code></div><br>and then use it as fol=
lowing:<br><br><div class=3D"prettyprint" style=3D"background-color: rgb(25=
0, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; border=
-width: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div class=
=3D"subprettyprint"><span style=3D"color: #000;" class=3D"styled-by-prettif=
y">sort</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify">vec</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">begin</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">(),</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> vec</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">.</span><span style=3D"color: #008;" class=3D"=
styled-by-prettify">end</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">(),</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> order_by</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">([](</span><span style=3D"color: #606;" class=3D"styled-by-prettify">P=
oint</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> p</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">){</span><span=
style=3D"color: #008;" class=3D"styled-by-prettify">return</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> make_tuple</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify">p</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">x</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> decreasing</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">p<=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify">y</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">));}));</span></div></code><=
/div><br>For the record: Another neat usecase (that I consider in the 10%) =
that works out of the box is ordering points in the plane in circular order=
around the origin:<br><br><div class=3D"prettyprint" style=3D"background-c=
olor: rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-style: s=
olid; border-width: 1px; word-wrap: break-word;"><code class=3D"prettyprint=
"><div class=3D"subprettyprint"><span style=3D"color: #000;" class=3D"style=
d-by-prettify">sort</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
vec</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</span=
><span style=3D"color: #008;" class=3D"styled-by-prettify">begin</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">(),</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> vec</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #008=
;" class=3D"styled-by-prettify">end</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">(),</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> order_by</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">([](</span><span style=3D"color: #606;" class=3D"styled-by=
-prettify">Point</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> p</span><span style=3D"color: #660;" class=3D"styled-by-prettify">){=
</span><span style=3D"color: #008;" class=3D"styled-by-prettify">return</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> atan2</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify">p</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify">y</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> p</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">x</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">);}));</span><=
/div></code></div><br>Best Regards <br>Ben Strasser<br><br><br></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_1150_29018663.1401522965781--
.
Author: Diggory Blake <diggsey@googlemail.com>
Date: Sat, 31 May 2014 06:33:07 -0700 (PDT)
Raw View
------=_Part_1058_27768170.1401543187096
Content-Type: text/plain; charset=UTF-8
Your example of a "decreasing" function is nice, although it would be
difficult to implement for all types the way you suggested. How about
instead of returning the same type as its argument, it constructs the
following:
template<typename T>
struct decreasing_t {
T m_value;
inline bool operator<(decreasing_t<T> const& other) { return m_value >
other.m_value; }
inline bool operator>(decreasing_t<T> const& other) { return m_value <
other.m_value; }
inline bool operator<=(decreasing_t<T> const& other) { return m_value >=
other.m_value; }
inline bool operator>=(decreasing_t<T> const& other) { return m_value <=
other.m_value; }
inline bool operator==(decreasing_t<T> const& other) { return m_value ==
other.m_value; }
inline bool operator!=(decreasing_t<T> const& other) { return m_value !=
other.m_value; }
};
template<typename T>
decreasing_t<T> decreasing(T const& value) { return { value }; }
template<typename T>
T decreasing(decreasing_t<T> const& value) { return value.m_value; }
This would work out of the box for any copyable type (and you could add
move versions too), and applying decreasing twice is the identity operation.
On Saturday, 31 May 2014 08:56:05 UTC+1, strass...@gmail.com wrote:
>
> Hi,
>
> To your first point, I was going to suggest that the second parameter to
>> order_by be a comparator that defaults to std::less, but the problem is the
>> same as the second point, it seems as though you would have to name the
>> type-id:
>>
>
> I do not like this for three reasons:
>
> 1) I so no benefit over adding a generally useful reverse_order function
> that could be used like this:
>
> sort(vec.begin(), vec.end(), reverse_order(order_by([](Point p){return p.x
> ;})));
>
> The benefit of reverse_order is that it works with every comparator as
> input (i.e. more general) and it is clear at the call site without the
> documentation what is going on.
>
> 2) You do not really solve the problem. You can not encode the
> requirement: "Sort first by x increasing and then by y decreasing". You can
> only encode "Sort first by x then by y both increasing" or "Sort first by x
> then by y both decreasing".
>
> 3) This is a pure convenience feature. There is nothing that can not be
> done without it. There is therefore no need to work in every imaginable
> border case. I strongly advocate keeping the syntax simple, even if this
> means that it is only usable in 90% of the cases. With simple syntax I mean
> among other things omitting parameters that are only used in 10% of the
> cases. IMO in 90% of the use cases things are ordered by int, floats and
> strings, either increasingly or decreasingly. For floats already the
> initial proposition is good enough:
>
> sort(vec.begin(), vec.end(), order_by([](Point p){return make_tuple(p.x, -
> p.y);}));
>
> This nearly also works for ints. I write nearly because of the problem
> that -numeric_limits<int>::min() == 0 leads to subtle bugs. We can however
> do the following for ints:
>
> sort(vec.begin(), vec.end(), order_by([](Point p){return make_tuple(p.x, ~
> p.y);}));
>
> This works but IMO it is way to easy to make a mistake here. For strings I
> unfortunately do not see such an easy method.
>
> Perhaps adding a decreasing function would be useful:
>
> int decreasing(int x){return ~x;}
> float decreasing(float x){return -x;}
> string decreasing(string x){for(auto&y:x)x=~x;return move(x);}
>
> and then use it as following:
>
> sort(vec.begin(), vec.end(), order_by([](Point p){return make_tuple(p.x,
> decreasing(p.y));}));
>
> For the record: Another neat usecase (that I consider in the 10%) that
> works out of the box is ordering points in the plane in circular order
> around the origin:
>
> sort(vec.begin(), vec.end(), order_by([](Point p){return atan2(p.y, p.x
> );}));
>
> Best Regards
> Ben Strasser
>
>
>
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_1058_27768170.1401543187096
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Your example of a "decreasing" function is nice, although =
it would be difficult to implement for all types the way you suggested. How=
about instead of returning the same type as its argument, it constructs th=
e following:<br><div class=3D"prettyprint" style=3D"background-color: rgb(2=
50, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; borde=
r-width: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div clas=
s=3D"subprettyprint"><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">template</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
"><</span><span style=3D"color: #008;" class=3D"styled-by-prettify">type=
name</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> T</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">></span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span sty=
le=3D"color: #008;" class=3D"styled-by-prettify">struct</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> decreasing_t </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"><br> T m_value</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"><br><br> </s=
pan><span style=3D"color: #008;" class=3D"styled-by-prettify">inline</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span sty=
le=3D"color: #008;" class=3D"styled-by-prettify">bool</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">operator</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify"><(</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify">decreasing_t</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify"><</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">T</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">></span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">=
const</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> other</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">return</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"> m_value </span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">></span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> other</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
>m_value</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br> </span><sp=
an style=3D"color: #008;" class=3D"styled-by-prettify">inline</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">bool</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">operator</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">>(</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify">decreasing_t</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify"><</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify">T</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">></span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">const<=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">&</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> other</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"=
styled-by-prettify">return</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> m_value </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify"><</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> other</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">m_va=
lue</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br> </span><span sty=
le=3D"color: #008;" class=3D"styled-by-prettify">inline</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">bool</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">operator</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify"><=3D(</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">decreasing_t</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify"><</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify">T</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">></span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">cons=
t</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> other</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">return</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> m_value </span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">>=3D</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> other</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
m_value</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br> </span><sp=
an style=3D"color: #008;" class=3D"styled-by-prettify">inline</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">bool</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">operator</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">>=3D(</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">decreasing_t</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify"><</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify">T</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">></span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">cons=
t</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> other</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">return</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> m_value </span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify"><=3D</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> other</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
m_value</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br> </span><sp=
an style=3D"color: #008;" class=3D"styled-by-prettify">inline</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">bool</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">operator</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">=3D=3D(</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">decreasing_t</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify"><</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify">T</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">></span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">cons=
t</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> other</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">return</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> m_value </span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">=3D=3D</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> other</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">m=
_value</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span=
style=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify"><br> </span><span =
style=3D"color: #008;" class=3D"styled-by-prettify">inline</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #008;" class=3D"styled-by-prettify">bool</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">operator</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">!=3D(</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify">decreasing_t</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify"><</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">T</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">></span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
</span><span style=3D"color: #008;" class=3D"styled-by-prettify">const</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">&</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> other</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">{</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"styl=
ed-by-prettify">return</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> m_value </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">!=3D</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> other</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">m_value<=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">};</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br><br></span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">template</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify"><</span><span style=3D"color: #008;" class=3D"st=
yled-by-prettify">typename</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> T</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">></span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"><br>decreasing_t</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify"><</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">T</span><span style=3D"color: #660;" class=3D"styled-by-prettify">></s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> decreasing</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify">T </span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">const</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">&</span><span style=3D"color:=
#000;" class=3D"styled-by-prettify"> value</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">ret=
urn</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> value </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">};</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;"=
class=3D"styled-by-prettify">}</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"><br></span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">template</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify"><</span><span style=3D"color: #008;" class=3D"styled-by-pre=
ttify">typename</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> T</span><span style=3D"color: #660;" class=3D"styled-by-prettify">>=
;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>T dec=
reasing</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify">decreasing_t=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify"><</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify">T</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">></span><span style=3D=
"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #=
008;" class=3D"styled-by-prettify">const</span><span style=3D"color: #660;"=
class=3D"styled-by-prettify">&</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> value</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </spa=
n><span style=3D"color: #008;" class=3D"styled-by-prettify">return</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> value</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify">m_value</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">}</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br></span></div></code></div><br>This would work out of the=
box for any copyable type (and you could add move versions too), and apply=
ing decreasing twice is the identity operation.<br><br>On Saturday, 31 May =
2014 08:56:05 UTC+1, strass...@gmail.com wrote:<blockquote class=3D"gmail_=
quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;pa=
dding-left: 1ex;"><div dir=3D"ltr">Hi,<br><br><blockquote class=3D"gmail_qu=
ote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding=
-left:1ex"><div dir=3D"ltr"><div><div class=3D"gmail_quote"><div>To your fi=
rst point, I was going to suggest that the second parameter to order_by be =
a comparator that defaults to std::less, but the problem is the same as the=
second point, it seems as though you would have to name the type-id:</div>=
</div></div></div></blockquote><div><br>I do not like this for three reason=
s:<br><br>1) I so no benefit over adding a generally useful reverse_order f=
unction that could be used like this:<br><br><div style=3D"background-color=
:rgb(250,250,250);border-color:rgb(187,187,187);border-style:solid;border-w=
idth:1px;word-wrap:break-word"><code><div><span style=3D"color:#000">sort</=
span><span style=3D"color:#660">(</span><span style=3D"color:#000">vec</spa=
n><span style=3D"color:#660">.</span><span style=3D"color:#008">begin</span=
><span style=3D"color:#660">(),</span><span style=3D"color:#000"> vec</span=
><span style=3D"color:#660">.</span><span style=3D"color:#008">end</span><s=
pan style=3D"color:#660">(),</span><span style=3D"color:#000"> reverse_orde=
r</span><span style=3D"color:#660">(</span><span style=3D"color:#000">order=
_by</span><span style=3D"color:#660">([](</span><span style=3D"color:#606">=
Poin<wbr>t</span><span style=3D"color:#000"> p</span><span style=3D"color:#=
660">){</span><span style=3D"color:#008">return</span><span style=3D"color:=
#000"> p</span><span style=3D"color:#660">.</span><span style=3D"color:#000=
">x</span><span style=3D"color:#660">;})));</span></div></code></div><br>Th=
e benefit of reverse_order is that it works with every comparator as input =
(i.e. more general) and it is clear at the call site without the documentat=
ion what is going on.<br> <br>2) You do not really solve the problem. =
You can not encode the requirement: "Sort first by x increasing and then by=
y decreasing". You can only encode "Sort first by x then by y both increas=
ing" or "Sort first by x then by y both decreasing".<br><br>3) This is a pu=
re convenience feature. There is nothing that can not be done without it. T=
here is therefore no need to work in every imaginable border case. I strong=
ly advocate keeping the syntax simple, even if this means that it is only u=
sable in 90% of the cases. With simple syntax I mean among other things omi=
tting parameters that are only used in 10% of the cases. IMO in 90% of the =
use cases things are ordered by int, floats and strings, either increasingl=
y or decreasingly. For floats already the initial proposition is good enoug=
h:<br><br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(=
187,187,187);border-style:solid;border-width:1px;word-wrap:break-word"><cod=
e><div><span style=3D"color:#000">sort</span><span style=3D"color:#660">(</=
span><span style=3D"color:#000">vec</span><span style=3D"color:#660">.</spa=
n><span style=3D"color:#008">begin</span><span style=3D"color:#660">(),</sp=
an><span style=3D"color:#000"> vec</span><span style=3D"color:#660">.</span=
><span style=3D"color:#008">end</span><span style=3D"color:#660">(),</span>=
<span style=3D"color:#000"> order_by</span><span style=3D"color:#660">([](<=
/span><span style=3D"color:#606">Point</span><span style=3D"color:#000"> p<=
/span><span style=3D"color:#660">){</span><span style=3D"color:#008">return=
</span><span style=3D"color:#000"> make_tuple</span><span style=3D"color:#6=
60">(</span><span style=3D"color:#000">p</span><span style=3D"color:#660">.=
</span><span style=3D"color:#000">x</span><span style=3D"color:#660">,</spa=
n><span style=3D"color:#000"> </span><span style=3D"color:#660">-</span><sp=
an style=3D"color:#000">p</span><span style=3D"color:#660">.</span><span st=
yle=3D"color:#000">y</span><span style=3D"color:#660">);}));</span></div></=
code></div><br>This nearly also works for ints. I write nearly because of t=
he problem that -numeric_limits<int>::min() =3D=3D 0 leads to subtle =
bugs. We can however do the following for ints:<br><br><div style=3D"backgr=
ound-color:rgb(250,250,250);border-color:rgb(187,187,187);border-style:soli=
d;border-width:1px;word-wrap:break-word"><code><div><span style=3D"color:#0=
00">sort</span><span style=3D"color:#660">(</span><span style=3D"color:#000=
">vec</span><span style=3D"color:#660">.</span><span style=3D"color:#008">b=
egin</span><span style=3D"color:#660">(),</span><span style=3D"color:#000">=
vec</span><span style=3D"color:#660">.</span><span style=3D"color:#008">en=
d</span><span style=3D"color:#660">(),</span><span style=3D"color:#000"> or=
der_by</span><span style=3D"color:#660">([](</span><span style=3D"color:#60=
6">Point</span><span style=3D"color:#000"> p</span><span style=3D"color:#66=
0">){</span><span style=3D"color:#008">return</span><span style=3D"color:#0=
00"> make_tuple</span><span style=3D"color:#660">(</span><span style=3D"col=
or:#000">p</span><span style=3D"color:#660">.</span><span style=3D"color:#0=
00">x</span><span style=3D"color:#660">,</span><span style=3D"color:#000"> =
</span><span style=3D"color:#660">~</span><span style=3D"color:#000">p</spa=
n><span style=3D"color:#660">.</span><span style=3D"color:#000">y</span><sp=
an style=3D"color:#660">);}));</span></div></code></div><br>This works but =
IMO it is way to easy to make a mistake here. For strings I unfortunately d=
o not see such an easy method.<br><br>Perhaps adding a decreasing function =
would be useful:<br><br><div style=3D"background-color:rgb(250,250,250);bor=
der-color:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:br=
eak-word"><code><div><span style=3D"color:#008">int</span><span style=3D"co=
lor:#000"> decreasing</span><span style=3D"color:#660">(</span><span style=
=3D"color:#008">int</span><span style=3D"color:#000"> x</span><span style=
=3D"color:#660">){</span><span style=3D"color:#008">return</span><span styl=
e=3D"color:#000"> </span><span style=3D"color:#660">~</span><span style=3D"=
color:#000">x</span><span style=3D"color:#660">;}</span><span style=3D"colo=
r:#000"><br></span><span style=3D"color:#008">float</span><span style=3D"co=
lor:#000"> decreasing</span><span style=3D"color:#660">(</span><span style=
=3D"color:#008">float</span><span style=3D"color:#000"> x</span><span style=
=3D"color:#660">){</span><span style=3D"color:#008">return</span><span styl=
e=3D"color:#000"> </span><span style=3D"color:#660">-</span><span style=3D"=
color:#000">x</span><span style=3D"color:#660">;}</span><span style=3D"colo=
r:#000"><br></span><span style=3D"color:#008">string</span><span style=3D"c=
olor:#000"> decreasing</span><span style=3D"color:#660">(</span><span style=
=3D"color:#008">string</span><span style=3D"color:#000"> x</span><span styl=
e=3D"color:#660">){</span><span style=3D"color:#008">for</span><span style=
=3D"color:#660">(</span><span style=3D"color:#008">auto</span><span style=
=3D"color:#660">&</span><span style=3D"color:#000">y</span><span style=
=3D"color:#660">:</span><span style=3D"color:#000">x</span><span style=3D"c=
olor:#660">)</span><span style=3D"color:#000">x</span><span style=3D"color:=
#660">=3D~</span><span style=3D"color:#000">x</span><span style=3D"color:#6=
60">;</span><span style=3D"color:#008">return</span><span style=3D"color:#0=
00"> move</span><span style=3D"color:#660">(</span><span style=3D"color:#00=
0">x</span><span style=3D"color:#660">);}</span><span style=3D"color:#000">=
<br></span></div></code></div><br>and then use it as following:<br><br><div=
style=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);b=
order-style:solid;border-width:1px;word-wrap:break-word"><code><div><span s=
tyle=3D"color:#000">sort</span><span style=3D"color:#660">(</span><span sty=
le=3D"color:#000">vec</span><span style=3D"color:#660">.</span><span style=
=3D"color:#008">begin</span><span style=3D"color:#660">(),</span><span styl=
e=3D"color:#000"> vec</span><span style=3D"color:#660">.</span><span style=
=3D"color:#008">end</span><span style=3D"color:#660">(),</span><span style=
=3D"color:#000"> order_by</span><span style=3D"color:#660">([](</span><span=
style=3D"color:#606">Point</span><span style=3D"color:#000"> p</span><span=
style=3D"color:#660">){</span><span style=3D"color:#008">return</span><spa=
n style=3D"color:#000"> make_tuple</span><span style=3D"color:#660">(</span=
><span style=3D"color:#000">p</span><span style=3D"color:#660">.</span><spa=
n style=3D"color:#000">x</span><span style=3D"color:#660">,</span><span sty=
le=3D"color:#000"> decreasing</span><span style=3D"color:#660">(</span><spa=
n style=3D"color:#000">p</span><span style=3D"color:#660">.</span><span sty=
le=3D"color:#000">y</span><span style=3D"color:#660">));}));</span></div></=
code></div><br>For the record: Another neat usecase (that I consider in the=
10%) that works out of the box is ordering points in the plane in circular=
order around the origin:<br><br><div style=3D"background-color:rgb(250,250=
,250);border-color:rgb(187,187,187);border-style:solid;border-width:1px;wor=
d-wrap:break-word"><code><div><span style=3D"color:#000">sort</span><span s=
tyle=3D"color:#660">(</span><span style=3D"color:#000">vec</span><span styl=
e=3D"color:#660">.</span><span style=3D"color:#008">begin</span><span style=
=3D"color:#660">(),</span><span style=3D"color:#000"> vec</span><span style=
=3D"color:#660">.</span><span style=3D"color:#008">end</span><span style=3D=
"color:#660">(),</span><span style=3D"color:#000"> order_by</span><span sty=
le=3D"color:#660">([](</span><span style=3D"color:#606">Point</span><span s=
tyle=3D"color:#000"> p</span><span style=3D"color:#660">){</span><span styl=
e=3D"color:#008">return</span><span style=3D"color:#000"> atan2</span><span=
style=3D"color:#660">(</span><span style=3D"color:#000">p</span><span styl=
e=3D"color:#660">.</span><span style=3D"color:#000">y</span><span style=3D"=
color:#660">,</span><span style=3D"color:#000"> p</span><span style=3D"colo=
r:#660">.</span><span style=3D"color:#000">x</span><span style=3D"color:#66=
0">);}));</span></div></code></div><br>Best Regards <br>Ben Strasser<br><br=
><br></div></div></blockquote></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_1058_27768170.1401543187096--
.
Author: =?UTF-8?Q?R=C3=B3bert_D=C3=A1vid?= <lrdxgm@gmail.com>
Date: Wed, 4 Jun 2014 13:05:27 -0700 (PDT)
Raw View
------=_Part_404_11361726.1401912327089
Content-Type: text/plain; charset=UTF-8
> This nearly also works for ints. I write nearly because of the problem
> that -numeric_limits<int>::min() == 0 leads to subtle bugs. We can however
> do the following for ints:
>
Totally sidenote, but -numeric_limits<int>::min() does not equal to 0, it
is a huge undefined behavior. Difference is, that an optimizer can do
anything with it. For instance, can make your computer catch fire; but
usually it will just give you 0. (Unsigned integers are defined for modulo
arithmetics.)
Regards, Robert
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_404_11361726.1401912327089
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><br><blockquote class=3D"gmail_quote" style=3D"margin: 0;m=
argin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=
=3D"ltr"><div>This nearly also works for ints. I write nearly because of th=
e problem that -numeric_limits<int>::min() =3D=3D 0 leads to subtle b=
ugs. We can however do the following for ints:<br></div></div></blockquote>=
<div dir=3D"ltr"><div><br>Totally sidenote, but -numeric_limits<int>:=
:min() does not equal to 0, it is a huge undefined behavior. Difference is,=
that an optimizer can do anything with it. For instance, can make your com=
puter catch fire; but usually it will just give you 0. (Unsigned integers a=
re defined for modulo arithmetics.)<br><br>Regards, Robert<br></div></div><=
/div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_404_11361726.1401912327089--
.
Author: strasser.ben@gmail.com
Date: Sun, 8 Jun 2014 14:06:38 -0700 (PDT)
Raw View
------=_Part_25_21507037.1402261598734
Content-Type: text/plain; charset=UTF-8
Hi,
below is what I currently have. The design decisions are provided as
comments inline. For ints its good enough for std::string its not yet quite
good enough because of the copy. Does anyone have a good idea how to avoid
this copy and not introduce any potential hard to find bugs?
template<class F>
class order_by_t{
public:
// By value and not by reference to support the std::map constuctor &
co work
explicit order_by_t(F f){
return std::move(f);
}
template<class T>
bool operator()(const T&l, const T&r)const{
return f(l) < f(r);
}
private:
F f;
};
template<class F>
order_by_t<F> order_by(F f){
return order_by_t{std::move(f)};
}
template<class T>
class decreasing_t{
public:
// By value to not fail badly on the following code:
// std::sort(std::begin(vec), std::end(vec), order_by([](Point
p){return decreasing(p.x);}));
// By reference requires that the lambda takes p by reference. However,
we can not guarentee
// this and if the user forgets it we have a very hard to find
explicit decreasing_t(T t):
t(std::move(t)){}
friend bool operator<(const decreasing_t&l, const decreasing_t&r){
return r < l;
}
// No operator<= because the only natural thing would be to implement
it in terms of >=.
// However, not all types that can be sorted provide >=. They only need
to provide operator<.
private:
T t;
};
template<class T>
decreasing_t<T>decreasing(T t){
return order_by_t{std::move(t)};
}
Best Regard,
Ben Strasser
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_25_21507037.1402261598734
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hi,<br><br>below is what I currently have. The design deci=
sions are provided as comments inline. For ints its good enough for std::st=
ring its not yet quite good enough because of the copy. Does anyone have a =
good idea how to avoid this copy and not introduce any potential hard to fi=
nd bugs?<br><br><div class=3D"prettyprint" style=3D"background-color: rgb(2=
50, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; borde=
r-width: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div clas=
s=3D"subprettyprint"><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">template</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
"><</span><span style=3D"color: #008;" class=3D"styled-by-prettify">clas=
s</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> F</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">></span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">class</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> order_by_t</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" c=
lass=3D"styled-by-prettify">public</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">:</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br> </span><span style=3D"color: #800;" class=
=3D"styled-by-prettify">// By value and not by reference to support the std=
::map constuctor & co work</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"><br> </span><span style=3D"color: #008;" c=
lass=3D"styled-by-prettify">explicit</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> order_by_t</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify">F f</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">){</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br> </span><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">return</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"> std</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify">move</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">f</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">);</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"><br> </span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">}</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> <br><br>&nbs=
p; </span><span style=3D"color: #008;" class=3D"styled-by-prettify">=
template</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&l=
t;</span><span style=3D"color: #008;" class=3D"styled-by-prettify">class</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> T</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">></span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br> </span><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">bool</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"co=
lor: #008;" class=3D"styled-by-prettify">operator</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">()(</span><span style=3D"color: #008=
;" class=3D"styled-by-prettify">const</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> T</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">&</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify">l</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </s=
pan><span style=3D"color: #008;" class=3D"styled-by-prettify">const</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> T</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">&</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify">r</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">)</span><span style=3D"color: #008;" cla=
ss=3D"styled-by-prettify">const</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"><br> </span><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">return</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify"> f</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">l</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #660;" class=3D"styled-by-prettify"><</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> f</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify">r</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">);</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br> </span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" class=3D"st=
yled-by-prettify">private</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">:</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"><br> F f</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">};</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><b=
r></span><span style=3D"color: #008;" class=3D"styled-by-prettify">template=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify"><</span=
><span style=3D"color: #008;" class=3D"styled-by-prettify">class</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> F</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">></span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br>order_by_t</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify"><</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify">F</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">></span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> order_by</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">F f</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">){</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
><br> </span><span style=3D"color: #008;" class=3D"styled-by-p=
rettify">return</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> order_by_t</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify">std=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify">move</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify">f</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">)};</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"><br><br></span><span style=3D"color: #008;" class=3D"styled-by-p=
rettify">template</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify"><</span><span style=3D"color: #008;" class=3D"styled-by-prettify"=
>class</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> T</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">></span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">class</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> decreasing_t</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #=
008;" class=3D"styled-by-prettify">public</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">:</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br> </span><span style=3D"color: #800=
;" class=3D"styled-by-prettify">// By value to not fail badly on the follow=
ing code:</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><=
br> </span><span style=3D"color: #800;" class=3D"styled-by-pre=
ttify">// std::sort(std::begin(vec), std::end(vec), order_by([](Poin=
t p){return decreasing(p.x);}));</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br> </span><span style=3D"color: #800=
;" class=3D"styled-by-prettify">// By reference requires that the lambda ta=
kes p by reference. However, we can not guarentee</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"><br> </span><span style=
=3D"color: #800;" class=3D"styled-by-prettify">// this and if the user forg=
ets it we have a very hard to find </span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"><br> </span><span style=3D"color: #00=
8;" class=3D"styled-by-prettify">explicit</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> decreasing_t</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify">T t</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">):</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"><br> t</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">std</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify">move</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">t</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">)){}</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"><br><br> =
</span><span style=3D"color: #008;" class=3D"styled-by-prettify">friend</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span=
style=3D"color: #008;" class=3D"styled-by-prettify">bool</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">operator</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify"><(</span><span style=3D"color: #008;=
" class=3D"styled-by-prettify">const</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> decreasing_t</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">&</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">l</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">con=
st</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> decreas=
ing_t</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify">r</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">){</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br> &n=
bsp; </span><span style=3D"color: #008;" class=3D"styled-by-prettify">retur=
n</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> r </span=
><span style=3D"color: #660;" class=3D"styled-by-prettify"><</span><span=
style=3D"color: #000;" class=3D"styled-by-prettify"> l</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br> </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br><br> </span><span st=
yle=3D"color: #800;" class=3D"styled-by-prettify">// No operator<=3D bec=
ause the only natural thing would be to implement it in terms of >=3D.</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br> &=
nbsp; </span><span style=3D"color: #800;" class=3D"styled-by-prettify">// H=
owever, not all types that can be sorted provide >=3D. They only need to=
provide operator<.</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"><br><br></span><span style=3D"color: #008;" class=3D"styled-by-=
prettify">private</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">:</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r> T t</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">;</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
<br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">};</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br></span=
><span style=3D"color: #008;" class=3D"styled-by-prettify">template</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify"><</span><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">class</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> T</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">></span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"><br>decreasing_t</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify"><</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify">T</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">></span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify">decreasing</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify">T t</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">){</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&n=
bsp; </span><span style=3D"color: #008;" class=3D"styled-by-prettify=
">return</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> o=
rder_by_t</span><span style=3D"color: #660;" class=3D"styled-by-prettify">{=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify">std</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify">move</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify">t</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">)};</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">}</span></div></code></div><br>Best Regard,<br>Ben Strasser<br=
><br></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_25_21507037.1402261598734--
.
Author: "'Geoffrey Romer' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Tue, 10 Jun 2014 11:23:31 -0700
Raw View
--001a11c139c8c6152b04fb7f6c90
Content-Type: text/plain; charset=UTF-8
This code looks ill-formed to me, to the point that I have a hard time
following what it's intended to do. It would probably be easier to have
this discussion about code that compiles and passes some basic tests.
On Sun, Jun 8, 2014 at 2:06 PM, <strasser.ben@gmail.com> wrote:
> Hi,
>
> below is what I currently have. The design decisions are provided as
> comments inline. For ints its good enough for std::string its not yet quite
> good enough because of the copy. Does anyone have a good idea how to avoid
> this copy and not introduce any potential hard to find bugs?
>
> template<class F>
> class order_by_t{
> public:
> // By value and not by reference to support the std::map constuctor &
> co work
> explicit order_by_t(F f){
> return std::move(f);
>
> }
>
> template<class T>
> bool operator()(const T&l, const T&r)const{
> return f(l) < f(r);
> }
> private:
> F f;
> };
>
> template<class F>
> order_by_t<F> order_by(F f){
> return order_by_t{std::move(f)};
> }
>
> template<class T>
> class decreasing_t{
> public:
> // By value to not fail badly on the following code:
> // std::sort(std::begin(vec), std::end(vec), order_by([](Point
> p){return decreasing(p.x);}));
> // By reference requires that the lambda takes p by reference.
> However, we can not guarentee
> // this and if the user forgets it we have a very hard to find
> explicit decreasing_t(T t):
> t(std::move(t)){}
>
> friend bool operator<(const decreasing_t&l, const decreasing_t&r){
> return r < l;
> }
>
> // No operator<= because the only natural thing would be to implement
> it in terms of >=.
> // However, not all types that can be sorted provide >=. They only
> need to provide operator<.
>
> private:
> T t;
> };
>
> template<class T>
> decreasing_t<T>decreasing(T t){
> return order_by_t{std::move(t)};
> }
>
> Best Regard,
> Ben Strasser
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> Visit this group at
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--001a11c139c8c6152b04fb7f6c90
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div class=3D"gmail_extra">This code looks ill-formed to m=
e, to the point that I have a hard time following what it's intended to=
do. It would probably be easier to have this discussion about code that co=
mpiles and passes some basic tests.</div>
<div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On Sun, Jun 8, 20=
14 at 2:06 PM, <span dir=3D"ltr"><<a href=3D"mailto:strasser.ben@gmail.=
com" target=3D"_blank">strasser.ben@gmail.com</a>></span> wrote:<br><blo=
ckquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #c=
cc solid;padding-left:1ex">
<div dir=3D"ltr">Hi,<br><br>below is what I currently have. The design deci=
sions are provided as comments inline. For ints its good enough for std::st=
ring its not yet quite good enough because of the copy. Does anyone have a =
good idea how to avoid this copy and not introduce any potential hard to fi=
nd bugs?<br>
<br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,18=
7,187);border-style:solid;border-width:1px;word-wrap:break-word"><code><div=
><span style=3D"color:#008">template</span><span style=3D"color:#660"><<=
/span><span style=3D"color:#008">class</span><span style=3D"color:#000"> F<=
/span><span style=3D"color:#660">></span><span style=3D"color:#000"><br>
</span><span style=3D"color:#008">class</span><span style=3D"color:#000"> o=
rder_by_t</span><span style=3D"color:#660">{</span><span style=3D"color:#00=
0"><br></span><span style=3D"color:#008">public</span><span style=3D"color:=
#660">:</span><span style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#800">// By value and not by refe=
rence to support the std::map constuctor & co work</span><span style=3D=
"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#008">explicit</=
span><span style=3D"color:#000"> order_by_t</span><span style=3D"color:#660=
">(</span><span style=3D"color:#000">F f</span><span style=3D"color:#660">)=
{</span><span style=3D"color:#000"><br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span>=
<span style=3D"color:#000"> std</span><span style=3D"color:#660">::</span><=
span style=3D"color:#000">move</span><span style=3D"color:#660">(</span><sp=
an style=3D"color:#000">f</span><span style=3D"color:#660">);</span><div cl=
ass=3D"">
<span style=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#6=
60">}</span><span style=3D"color:#000"> =C2=A0 =C2=A0<br><br>=C2=A0 =C2=A0 =
</span><span style=3D"color:#008">template</span><span style=3D"color:#660"=
><</span><span style=3D"color:#008">class</span><span style=3D"color:#00=
0"> T</span><span style=3D"color:#660">></span><span style=3D"color:#000=
"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">bool</span><span style=3D"c=
olor:#000"> </span><span style=3D"color:#008">operator</span><span style=3D=
"color:#660">()(</span><span style=3D"color:#008">const</span><span style=
=3D"color:#000"> T</span><span style=3D"color:#660">&</span><span style=
=3D"color:#000">l</span><span style=3D"color:#660">,</span><span style=3D"c=
olor:#000"> </span><span style=3D"color:#008">const</span><span style=3D"co=
lor:#000"> T</span><span style=3D"color:#660">&</span><span style=3D"co=
lor:#000">r</span><span style=3D"color:#660">)</span><span style=3D"color:#=
008">const</span><span style=3D"color:#660">{</span><span style=3D"color:#0=
00"><br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span>=
<span style=3D"color:#000"> f</span><span style=3D"color:#660">(</span><spa=
n style=3D"color:#000">l</span><span style=3D"color:#660">)</span><span sty=
le=3D"color:#000"> </span><span style=3D"color:#660"><</span><span style=
=3D"color:#000"> f</span><span style=3D"color:#660">(</span><span style=3D"=
color:#000">r</span><span style=3D"color:#660">);</span><span style=3D"colo=
r:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#660">}</span><span style=3D"colo=
r:#000"><br></span></div><span style=3D"color:#008">private</span><span sty=
le=3D"color:#660">:</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 F f<=
/span><span style=3D"color:#660">;</span><span style=3D"color:#000"><br>
</span><span style=3D"color:#660">};</span><span style=3D"color:#000"><br><=
br></span><span style=3D"color:#008">template</span><span style=3D"color:#6=
60"><</span><span style=3D"color:#008">class</span><span style=3D"color:=
#000"> F</span><span style=3D"color:#660">></span><span style=3D"color:#=
000"><br>
order_by_t</span><span style=3D"color:#660"><</span><span style=3D"color=
:#000">F</span><span style=3D"color:#660">></span><span style=3D"color:#=
000"> order_by</span><span style=3D"color:#660">(</span><span style=3D"colo=
r:#000">F f</span><span style=3D"color:#660">){</span><span style=3D"color:=
#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span><span style=3D=
"color:#000"> order_by_t</span><span style=3D"color:#660">{</span><span sty=
le=3D"color:#000">std</span><span style=3D"color:#660">::</span><span style=
=3D"color:#000">move</span><span style=3D"color:#660">(</span><span style=
=3D"color:#000">f</span><span style=3D"color:#660">)};</span><span style=3D=
"color:#000"><br>
</span><span style=3D"color:#660">}</span><span style=3D"color:#000"><br><b=
r></span><span style=3D"color:#008">template</span><span style=3D"color:#66=
0"><</span><span style=3D"color:#008">class</span><span style=3D"color:#=
000"> T</span><span style=3D"color:#660">></span><span style=3D"color:#0=
00"><br>
</span><span style=3D"color:#008">class</span><span style=3D"color:#000"> d=
ecreasing_t</span><span style=3D"color:#660">{</span><span style=3D"color:#=
000"><br></span><span style=3D"color:#008">public</span><span style=3D"colo=
r:#660">:</span><span style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#800">// By value to not fail bad=
ly on the following code:</span><span style=3D"color:#000"><br>=C2=A0 =C2=
=A0 </span><span style=3D"color:#800">// =C2=A0 std::sort(std::begin(vec), =
std::end(vec), order_by([](Point p){return decreasing(p.x);}));</span><span=
style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#800">// By reference requires th=
at the lambda takes p by reference. However, we can not guarentee</span><sp=
an style=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#800"=
>// this and if the user forgets it we have a very hard to find </span><spa=
n style=3D"color:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">explicit</span><span style=
=3D"color:#000"> decreasing_t</span><span style=3D"color:#660">(</span><spa=
n style=3D"color:#000">T t</span><span style=3D"color:#660">):</span><span =
style=3D"color:#000"><br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 t</span><span style=3D"color:#660">(</span><spa=
n style=3D"color:#000">std</span><span style=3D"color:#660">::</span><span =
style=3D"color:#000">move</span><span style=3D"color:#660">(</span><span st=
yle=3D"color:#000">t</span><span style=3D"color:#660">)){}</span><span styl=
e=3D"color:#000"><br>
<br>=C2=A0 =C2=A0 </span><span style=3D"color:#008">friend</span><span styl=
e=3D"color:#000"> </span><span style=3D"color:#008">bool</span><span style=
=3D"color:#000"> </span><span style=3D"color:#008">operator</span><span sty=
le=3D"color:#660"><(</span><span style=3D"color:#008">const</span><span =
style=3D"color:#000"> decreasing_t</span><span style=3D"color:#660">&</=
span><span style=3D"color:#000">l</span><span style=3D"color:#660">,</span>=
<span style=3D"color:#000"> </span><span style=3D"color:#008">const</span><=
span style=3D"color:#000"> decreasing_t</span><span style=3D"color:#660">&a=
mp;</span><span style=3D"color:#000">r</span><span style=3D"color:#660">){<=
/span><span style=3D"color:#000"><br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span>=
<span style=3D"color:#000"> r </span><span style=3D"color:#660"><</span>=
<span style=3D"color:#000"> l</span><span style=3D"color:#660">;</span><spa=
n style=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#660">=
}</span><span style=3D"color:#000"><br>
<br>=C2=A0 =C2=A0 </span><span style=3D"color:#800">// No operator<=3D b=
ecause the only natural thing would be to implement it in terms of >=3D.=
</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"c=
olor:#800">// However, not all types that can be sorted provide >=3D. Th=
ey only need to provide operator<.</span><span style=3D"color:#000"><br>
<br></span><span style=3D"color:#008">private</span><span style=3D"color:#6=
60">:</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 T t</span><span st=
yle=3D"color:#660">;</span><span style=3D"color:#000"><br></span><span styl=
e=3D"color:#660">};</span><span style=3D"color:#000"><br>
<br></span><span style=3D"color:#008">template</span><span style=3D"color:#=
660"><</span><span style=3D"color:#008">class</span><span style=3D"color=
:#000"> T</span><span style=3D"color:#660">></span><span style=3D"color:=
#000"><br>
decreasing_t</span><span style=3D"color:#660"><</span><span style=3D"col=
or:#000">T</span><span style=3D"color:#660">></span><span style=3D"color=
:#000">decreasing</span><span style=3D"color:#660">(</span><span style=3D"c=
olor:#000">T t</span><span style=3D"color:#660">){</span><span style=3D"col=
or:#000"><br>
=C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span><span style=3D=
"color:#000"> order_by_t</span><span style=3D"color:#660">{</span><span sty=
le=3D"color:#000">std</span><span style=3D"color:#660">::</span><span style=
=3D"color:#000">move</span><span style=3D"color:#660">(</span><span style=
=3D"color:#000">t</span><span style=3D"color:#660">)};</span><span style=3D=
"color:#000"><br>
</span><span style=3D"color:#660">}</span></div></code></div><br>Best Regar=
d,<br>Ben Strasser<br><br></div><div class=3D"HOEnZb"><div class=3D"h5">
<p></p>
-- <br>
<br>
--- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br>
</div></div></blockquote></div><br></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--001a11c139c8c6152b04fb7f6c90--
.