Topic: Proposal to add std::mem_to_fun
Author: dacamara.cameron@gmail.com
Date: Tue, 19 Nov 2013 20:41:15 -0800 (PST)
Raw View
------=_Part_2561_13557628.1384922475031
Content-Type: text/plain; charset=ISO-8859-1
So I noticed browsing <functional> that there is currently not a way to
create a function object that can return just a member of an object. There
is a structure that converts a member function into a function object
callable via the class as an argument (std::mem_fun_t). My question is why
can't this exist for accessing members of a POD struct or even a class?
That could be useful for standard algorithms when you need access to a
specific member but don't wanna write out the lambda for it, eg:
struct Books {
std::string author;
std::string title;
};
// all we want is to make a set of titles of books from our library vector
std::set<std::string> uniqTitles;
std::transform(library.begin(), library.end(), std::inserter(uniqTitles,uniqTitles
..end()),
[] (const Books& b) -> std::string
{
return b.title;
}
); // why can't there be a standard way to just grab the member?
so really the implementation / interface would look a lot like std::mem_fun:
template <typename T, typename M>
struct mem_to_fun_t {
M T::*_mem;
explicit mem_to_fun_t(M T::*mem): _mem(mem) { }
const M& operator()(const T& c) const{return c.*_mem;}
M& operator()(T& c) {return c.*_mem;}
};
template <typename T, typename M> inline
auto mem_to_fun(M T::*mem) -> mem_to_fun_t<T,M> {
return mem_to_fun_t<T,M>{mem};
}
it doesn't have to be exactly like this but perhaps a similar
implementation.
Here is the ideone link if you wanna see how it could be used:
http://ideone.com/3uoDPV
--
---
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_2561_13557628.1384922475031
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">So I noticed browsing <functional> that there is cur=
rently not a way to create a function object that can return just a member =
of an object. There is a structure that converts a member function into a f=
unction object callable via the class as an argument (std::mem_fun_t). My q=
uestion is why can't this exist for accessing members of a POD struct or ev=
en a class? That could be useful for standard algorithms when you need acce=
ss to a specific member but don't wanna write out the lambda for it, eg:<br=
><br><div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 25=
0); border: 1px solid rgb(187, 187, 187); word-wrap: break-word;"><code cla=
ss=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: #008=
;" class=3D"styled-by-prettify">struct</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> </span><span style=3D"color: #606;" class=3D"s=
tyled-by-prettify">Books</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
::</span><span style=3D"color: #008;" class=3D"styled-by-prettify">string</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> author</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"><br> std</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">string</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> title</span><span style=3D"colo=
r: #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"styl=
ed-by-prettify"><br><br><br></span><span style=3D"color: #800;" class=3D"st=
yled-by-prettify">// all we want is to make a set of titles of books from o=
ur library vector</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"><br>std</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">::</span><span style=3D"color: #008;" class=3D"styled-by-prettify">set<=
/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 s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">string</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">></span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> uniqTitles</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"><br>std</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">transform</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify">library</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">.</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"> library</span><sp=
an 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"col=
or: #660;" class=3D"styled-by-prettify">(),</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> std</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify">inserter</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify">uniqTitles</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
uniqTitles</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"> <br> &nb=
sp; </span><span style=3D"color: #660;" c=
lass=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: #008;" class=3D"styled-by-prettify=
">const</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #606;" class=3D"styled-by-prettify">Books</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">&</span><span=
style=3D"color: #000;" class=3D"styled-by-prettify"> b</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"> std</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">::</span><span style=3D"color: #008;" class=3D"styled-by-=
prettify">string</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> <br> </span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br> &nb=
sp; </span><span style=3D"color: #008;" =
class=3D"styled-by-prettify">return</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> b</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">.</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify">title</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&n=
bsp; </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: #660;" c=
lass=3D"styled-by-prettify">);</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> </span><span style=3D"color: #800;" class=3D"styled-by=
-prettify">// why can't there be a standard way to just grab the member? </=
span></div></code></div><div><br></div><div>so really the implementation / =
interface would look a lot like std::mem_fun:</div><div><br></div><div><div=
class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); borde=
r: 1px solid rgb(187, 187, 187); word-wrap: break-word;"><code class=3D"pre=
ttyprint"><div class=3D"subprettyprint"><div class=3D"prettyprint" style=3D=
"background-color: rgb(250, 250, 250); border: 1px solid rgb(187, 187, 187)=
; word-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subpret=
typrint"><span style=3D"color: #008;" class=3D"styled-by-prettify">template=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify"><</span><span st=
yle=3D"color: #008;" class=3D"styled-by-prettify">typename</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> T</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: #008;" class=
=3D"styled-by-prettify">typename</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> M</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">></span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"><br></span><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">struct</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
mem_to_fun_t </span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&=
nbsp; M T</span><span style=3D"color: #660;" class=3D"styled-by-prettify">:=
:*</span><span style=3D"color: #000;" class=3D"styled-by-prettify">_mem</sp=
an><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">explicit</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> mem_to_fun_t</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">M T</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">::*</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify">mem</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">):</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> _mem</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy">mem</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"> </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"co=
lor: #008;" class=3D"styled-by-prettify">const</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> M</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">&</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">operator</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">()(</span><span style=3D"color: #008;" class=3D"styled-by-pret=
tify">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"color: #000;" class=3D"styled-by-prettify"> c</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=
: #008;" class=3D"styled-by-prettify">const</span><span style=3D"color: #66=
0;" 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"> c</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">.*</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy">_mem</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;}=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br> =
M</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: #008;" class=3D"styled-by-prettify">operator</span><span s=
tyle=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"styled-by-prettify">&</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify"> c</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #660;" 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"> c</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">.*</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify">_mem</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">;}</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">};</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"><br><br><br></span><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">template</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify"><</span><span style=3D"color: #008;" class=3D"styled-by-=
prettify">typename</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> T</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">typename</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> M</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">></span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008=
;" class=3D"styled-by-prettify">inline</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">auto</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> mem_to_fun</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify">M T</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">::*</span><span style=3D"color: #000;" class=3D"styled-by-prettify">mem<=
/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"c=
olor: #000;" class=3D"styled-by-prettify"> mem_to_fun_t</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify"><</span><span style=3D"co=
lor: #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">M</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: #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"> mem_to_fun_=
t</span><span style=3D"color: #660;" class=3D"styled-by-prettify"><</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify">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">M</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">>{</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify">mem</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-pre=
ttify">}</span></div></code></div></div></code></div><br></div><div>it does=
n't have to be exactly like this but perhaps a similar implementation.</div=
><div><br></div><div>Here is the ideone link if you wanna see how it could =
be used: <a href=3D"http://ideone.com/3uoDPV">http://ideone.com/3uoDPV=
</a></div><div><br></div><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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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_2561_13557628.1384922475031--
.
Author: "Billy O'Neal" <billy.oneal@gmail.com>
Date: Tue, 19 Nov 2013 21:19:16 -0800
Raw View
--001a1133073a9a639504eb94eebd
Content-Type: text/plain; charset=ISO-8859-1
IMHO, this could go either way. I've never gone to a standards meeting or
anything like that, but it seems the committee seems against messing with
additional binders now that lambdas are in the core language.
I'm not sure the name "mem_to_fun" makes sense -- the name "mem_fun" refers
to making a binder to a "MEMber_FUNction" -- but you're not binding to a
function at all here. This smooths over the syntactical issue of member
function calls needing the syntax x->y, but the STL only accepting the
syntax y(x).
Perhaps the name you're looking for is "project" -- because what you're
actually doing here is a projection.
An alternate solution proposed by Sean Parent in his Going::Native 2013
talk involves defining different versions of the standard algorithms that
accept optional projection functions as parameters.
Billy O'Neal
https://github.com/BillyONeal/ <https://bitbucket.org/BillyONeal/>
http://stackoverflow.com/users/82320/billy-oneal
Malware Response Instructor - BleepingComputer.com
On Tue, Nov 19, 2013 at 8:41 PM, <dacamara.cameron@gmail.com> wrote:
> So I noticed browsing <functional> that there is currently not a way to
> create a function object that can return just a member of an object. There
> is a structure that converts a member function into a function object
> callable via the class as an argument (std::mem_fun_t). My question is why
> can't this exist for accessing members of a POD struct or even a class?
> That could be useful for standard algorithms when you need access to a
> specific member but don't wanna write out the lambda for it, eg:
>
> struct Books {
> std::string author;
> std::string title;
> };
>
>
> // all we want is to make a set of titles of books from our library vector
> std::set<std::string> uniqTitles;
> std::transform(library.begin(), library.end(), std::inserter(uniqTitles,uniqTitles
> .end()),
> [] (const Books& b) -> std::string
> {
> return b.title;
> }
> ); // why can't there be a standard way to just grab the member?
>
> so really the implementation / interface would look a lot like
> std::mem_fun:
>
> template <typename T, typename M>
> struct mem_to_fun_t {
> M T::*_mem;
> explicit mem_to_fun_t(M T::*mem): _mem(mem) { }
>
> const M& operator()(const T& c) const{return c.*_mem;}
> M& operator()(T& c) {return c.*_mem;}
> };
>
>
> template <typename T, typename M> inline
> auto mem_to_fun(M T::*mem) -> mem_to_fun_t<T,M> {
> return mem_to_fun_t<T,M>{mem};
> }
>
> it doesn't have to be exactly like this but perhaps a similar
> implementation.
>
> Here is the ideone link if you wanna see how it could be used:
> http://ideone.com/3uoDPV
>
>
> --
>
> ---
> 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/.
--001a1133073a9a639504eb94eebd
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">IMHO, this could go either way. I've never gone to a s=
tandards meeting or anything like that, but it seems the committee seems ag=
ainst messing with additional binders now that lambdas are in the core lang=
uage.<div>
<br></div><div>I'm not sure the name "mem_to_fun" makes sense=
-- the name "mem_fun" refers to making a binder to a "MEMbe=
r_FUNction" -- but you're not binding to a function at all here. T=
his smooths over the syntactical issue of member function calls needing the=
syntax x->y, but the STL only accepting the syntax y(x).</div>
<div><br></div><div>Perhaps the name you're looking for is "projec=
t" -- because what you're actually doing here is a projection.</di=
v><div><br></div><div>An alternate solution proposed by Sean Parent in his =
Going::Native 2013 talk involves defining different versions of the standar=
d algorithms that accept optional projection functions as parameters.</div>
</div><div class=3D"gmail_extra"><br clear=3D"all"><div><div dir=3D"ltr"><d=
iv>Billy O'Neal</div><div><a href=3D"https://bitbucket.org/BillyONeal/"=
target=3D"_blank">https://github.com/BillyONeal/</a></div><div><a href=3D"=
http://stackoverflow.com/users/82320/billy-oneal" target=3D"_blank">http://=
stackoverflow.com/users/82320/billy-oneal</a></div>
<div>Malware Response Instructor - BleepingComputer.com</div></div></div>
<br><br><div class=3D"gmail_quote">On Tue, Nov 19, 2013 at 8:41 PM, <span =
dir=3D"ltr"><<a href=3D"mailto:dacamara.cameron@gmail.com" target=3D"_bl=
ank">dacamara.cameron@gmail.com</a>></span> wrote:<br><blockquote class=
=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd=
ing-left:1ex">
<div dir=3D"ltr">So I noticed browsing <functional> that there is cur=
rently not a way to create a function object that can return just a member =
of an object. There is a structure that converts a member function into a f=
unction object callable via the class as an argument (std::mem_fun_t). My q=
uestion is why can't this exist for accessing members of a POD struct o=
r even a class? That could be useful for standard algorithms when you need =
access to a specific member but don't wanna write out the lambda for it=
, eg:<br>
<br><div style=3D"background-color:rgb(250,250,250);border:1px solid rgb(18=
7,187,187);word-wrap:break-word"><code><div><span style=3D"color:#008">stru=
ct</span><span style> </span><span style=3D"color:#606">Books</span><span s=
tyle> </span><span style=3D"color:#660">{</span><span style><br>
=A0 std</span><span style=3D"color:#660">::</span><span style=3D"color:#008=
">string</span><span style> author</span><span style=3D"color:#660">;</span=
><span style><br>=A0 std</span><span style=3D"color:#660">::</span><span st=
yle=3D"color:#008">string</span><span style> title</span><span style=3D"col=
or:#660">;</span><span style><br>
</span><span style=3D"color:#660">};</span><span style><br><br><br></span><=
span style=3D"color:#800">// all we want is to make a set of titles of book=
s from our library vector</span><span style><br>std</span><span style=3D"co=
lor:#660">::</span><span style=3D"color:#008">set</span><span style=3D"colo=
r:#660"><</span><span style>std</span><span style=3D"color:#660">::</spa=
n><span style=3D"color:#008">string</span><span style=3D"color:#660">></=
span><span style> uniqTitles</span><span style=3D"color:#660">;</span><span=
style><br>
std</span><span style=3D"color:#660">::</span><span style>transform</span><=
span style=3D"color:#660">(</span><span style>library</span><span style=3D"=
color:#660">.</span><span style=3D"color:#008">begin</span><span style=3D"c=
olor:#660">(),</span><span style> library</span><span style=3D"color:#660">=
..</span><span style=3D"color:#008">end</span><span style=3D"color:#660">(),=
</span><span style> std</span><span style=3D"color:#660">::</span><span sty=
le>inserter</span><span style=3D"color:#660">(</span><span style>uniqTitles=
</span><span style=3D"color:#660">,</span><span style> uniqTitles</span><sp=
an style=3D"color:#660">.</span><span style=3D"color:#008">end</span><span =
style=3D"color:#660">()),</span><span style> <br>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0</span><span style=3D"color:#660">[]</span><=
span style> </span><span style=3D"color:#660">(</span><span style=3D"color:=
#008">const</span><span style> </span><span style=3D"color:#606">Books</spa=
n><span style=3D"color:#660">&</span><span style> b</span><span style=
=3D"color:#660">)</span><span style> </span><span style=3D"color:#660">->=
;</span><span style> std</span><span style=3D"color:#660">::</span><span st=
yle=3D"color:#008">string</span><span style> <br>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0</span><span style=3D"color:#660">{</span><s=
pan style><br>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 </span><span style=3D"col=
or:#008">return</span><span style> b</span><span style=3D"color:#660">.</sp=
an><span style>title</span><span style=3D"color:#660">;</span><span style><=
br>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0</span><span style=3D"color:#660">}</span><s=
pan style><br></span><span style=3D"color:#660">);</span><span style> </spa=
n><span style=3D"color:#800">// why can't there be a standard way to ju=
st grab the member? </span></div>
</code></div><div><br></div><div>so really the implementation / interface w=
ould look a lot like std::mem_fun:</div><div><br></div><div><div style=3D"b=
ackground-color:rgb(250,250,250);border:1px solid rgb(187,187,187);word-wra=
p:break-word">
<code><div><div style=3D"background-color:rgb(250,250,250);border:1px solid=
rgb(187,187,187);word-wrap:break-word"><code><div><span style=3D"color:#00=
8">template</span><span style> </span><span style=3D"color:#660"><</span=
><span style=3D"color:#008">typename</span><span style> T</span><span style=
=3D"color:#660">,</span><span style> </span><span style=3D"color:#008">type=
name</span><span style> M</span><span style=3D"color:#660">></span><span=
style><br>
</span><span style=3D"color:#008">struct</span><span style> mem_to_fun_t </=
span><span style=3D"color:#660">{</span><span style><br>=A0 M T</span><span=
style=3D"color:#660">::*</span><span style>_mem</span><span style=3D"color=
:#660">;</span><span style><br>
=A0 </span><span style=3D"color:#008">explicit</span><span style> mem_to_fu=
n_t</span><span style=3D"color:#660">(</span><span style>M T</span><span st=
yle=3D"color:#660">::*</span><span style>mem</span><span style=3D"color:#66=
0">):</span><span style> _mem</span><span style=3D"color:#660">(</span><spa=
n style>mem</span><span style=3D"color:#660">)</span><span style> </span><s=
pan style=3D"color:#660">{</span><span style> </span><span style=3D"color:#=
660">}</span><span style><br>
=A0 <br>=A0 </span><span style=3D"color:#008">const</span><span style> M</s=
pan><span style=3D"color:#660">&</span><span style> </span><span style=
=3D"color:#008">operator</span><span style=3D"color:#660">()(</span><span s=
tyle=3D"color:#008">const</span><span style> T</span><span style=3D"color:#=
660">&</span><span style> c</span><span style=3D"color:#660">)</span><s=
pan style> </span><span style=3D"color:#008">const</span><span style=3D"col=
or:#660">{</span><span style=3D"color:#008">return</span><span style> c</sp=
an><span style=3D"color:#660">.*</span><span style>_mem</span><span style=
=3D"color:#660">;}</span><span style><br>
=A0 M</span><span style=3D"color:#660">&</span><span style> </span><spa=
n style=3D"color:#008">operator</span><span style=3D"color:#660">()(</span>=
<span style>T</span><span style=3D"color:#660">&</span><span style> c</=
span><span style=3D"color:#660">)</span><span style> </span><span style=3D"=
color:#660">{</span><span style=3D"color:#008">return</span><span style> c<=
/span><span style=3D"color:#660">.*</span><span style>_mem</span><span styl=
e=3D"color:#660">;}</span><span style><br>
</span><span style=3D"color:#660">};</span><span style><br><br><br></span><=
span style=3D"color:#008">template</span><span style> </span><span style=3D=
"color:#660"><</span><span style=3D"color:#008">typename</span><span sty=
le> T</span><span style=3D"color:#660">,</span><span style> </span><span st=
yle=3D"color:#008">typename</span><span style> M</span><span style=3D"color=
:#660">></span><span style> </span><span style=3D"color:#008">inline</sp=
an><span style><br>
</span><span style=3D"color:#008">auto</span><span style> mem_to_fun</span>=
<span style=3D"color:#660">(</span><span style>M T</span><span style=3D"col=
or:#660">::*</span><span style>mem</span><span style=3D"color:#660">)</span=
><span style> </span><span style=3D"color:#660">-></span><span style> me=
m_to_fun_t</span><span style=3D"color:#660"><</span><span style>T</span>=
<span style=3D"color:#660">,</span><span style>M</span><span style=3D"color=
:#660">></span><span style> </span><span style=3D"color:#660">{</span><s=
pan style><br>
=A0 </span><span style=3D"color:#008">return</span><span style> mem_to_fun_=
t</span><span style=3D"color:#660"><</span><span style>T</span><span sty=
le=3D"color:#660">,</span><span style>M</span><span style=3D"color:#660">&g=
t;{</span><span style>mem</span><span style=3D"color:#660">};</span><span s=
tyle><br>
</span><span style=3D"color:#660">}</span></div></code></div></div></code><=
/div><br></div><div>it doesn't have to be exactly like this but perhaps=
a similar implementation.</div><div><br></div><div>Here is the ideone link=
if you wanna see how it could be used:=A0<a href=3D"http://ideone.com/3uoD=
PV" target=3D"_blank">http://ideone.com/3uoDPV</a></div>
<span class=3D"HOEnZb"><font color=3D"#888888"><div><br></div><div><br></di=
v></font></span></div><span class=3D"HOEnZb"><font color=3D"#888888">
<p></p>
-- <br>
=A0<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%2Bunsubscribe@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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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 />
--001a1133073a9a639504eb94eebd--
.
Author: David Krauss <potswa@gmail.com>
Date: Wed, 20 Nov 2013 13:20:29 +0800
Raw View
On 11/20/13 12:41 PM, dacamara.cameron@gmail.com wrote:
> So I noticed browsing <functional> that there is currently not a way to
> create a function object that can return just a member of an object.
You can do this with std::bind, because member pointers are invokable.
auto fn = std::bind( & Books::author, std::placeholders::_1 );
The resulting bind object is a functor taking either a pointer or
reference to Book.
--
---
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: Jeffrey Yasskin <jyasskin@google.com>
Date: Wed, 20 Nov 2013 00:51:58 -0500
Raw View
I think std::mem_fn already supports this:
http://en.cppreference.com/w/cpp/utility/functional/mem_fn
On Tue, Nov 19, 2013 at 11:41 PM, <dacamara.cameron@gmail.com> wrote:
> So I noticed browsing <functional> that there is currently not a way to
> create a function object that can return just a member of an object. There
> is a structure that converts a member function into a function object
> callable via the class as an argument (std::mem_fun_t). My question is why
> can't this exist for accessing members of a POD struct or even a class? That
> could be useful for standard algorithms when you need access to a specific
> member but don't wanna write out the lambda for it, eg:
>
> struct Books {
> std::string author;
> std::string title;
> };
>
>
> // all we want is to make a set of titles of books from our library vector
> std::set<std::string> uniqTitles;
> std::transform(library.begin(), library.end(), std::inserter(uniqTitles,
> uniqTitles.end()),
> [] (const Books& b) -> std::string
> {
> return b.title;
> }
> ); // why can't there be a standard way to just grab the member?
>
> so really the implementation / interface would look a lot like std::mem_fun:
>
> template <typename T, typename M>
> struct mem_to_fun_t {
> M T::*_mem;
> explicit mem_to_fun_t(M T::*mem): _mem(mem) { }
>
> const M& operator()(const T& c) const{return c.*_mem;}
> M& operator()(T& c) {return c.*_mem;}
> };
>
>
> template <typename T, typename M> inline
> auto mem_to_fun(M T::*mem) -> mem_to_fun_t<T,M> {
> return mem_to_fun_t<T,M>{mem};
> }
>
> it doesn't have to be exactly like this but perhaps a similar
> implementation.
>
> Here is the ideone link if you wanna see how it could be used:
> http://ideone.com/3uoDPV
>
>
> --
>
> ---
> 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/.
.