Topic: Add an iterator type to iterate structure members


Author: Sal Mast <salmast303@gmail.com>
Date: Sun, 1 Dec 2013 16:45:22 -0800 (PST)
Raw View
------=_Part_2708_15418323.1385945122880
Content-Type: text/plain; charset=ISO-8859-1

I don't follow proposals at all, and my C++ skills aren't so good, so many
apologies if this is already easily possible or proposed, but would anyone
else be interested in having an iterator adapter type that would iterate
over the members of classes? The class would store another iterator (a real
iterator from the container being iterated) and a pointer-to-member, and
when it was dereferenced, it would dereference the iterator it is storing
and apply the pointer-to-member and return a reference to it.

I often come upon circumstances where I want to apply algorithms on the
data members of types in a container, but I have to write the algorithm
manually or write a lambda. Again, this could be due to my poor C++ skills
and there could be a simple solution that I am unaware of, but if not, I
think a member iterator adapter would allow better utilization of standard
algorithms. An example of the use of it would be:

struct my_struct {
    // some members
    int id;
    // more members
};

std::list<my_struct> structs(100);

std::iota(make_member_iterator(begin(structs), &my_struct::id), {end(structs
)}, 1);

So you can do it without having to write a manual loop. You can also make
things like std::find work without writing a lambda

std::find(make_member_iterator(begin(structs), &my_struct::id), {end(structs
)}, 44);

Both of which I think look better than their counterparts.

The implementation would be very simple (I can provide an example
implementation if it's helpful but I doubt I could catch the nuances of
doing something like this) but I think it would be helpful enough to
warrant being included in the standard library.

--

---
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_2708_15418323.1385945122880
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I don't follow proposals at all, and my C++ skills aren't =
so good, so many apologies if this is already easily possible or proposed, =
but would anyone else be interested in having an iterator adapter type that=
 would iterate over the members of classes? The class would store another i=
terator (a real iterator from the container being iterated) and a pointer-t=
o-member, and when it was dereferenced, it would dereference the iterator i=
t is storing and apply the pointer-to-member and return a reference to it.<=
div><br></div><div>I often come upon circumstances where I want to apply al=
gorithms on the data members of types in a container, but I have to write t=
he algorithm manually or write a lambda. Again, this could be due to my poo=
r C++ skills and there could be a simple solution that I am unaware of, but=
 if not, I think a member iterator adapter would allow better utilization o=
f standard algorithms. An example of the use of it would be:</div><div><div=
><br></div><div class=3D"prettyprint" style=3D"background-color: rgb(250, 2=
50, 250); border: 1px solid rgb(187, 187, 187); word-wrap: break-word;"><co=
de class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"color=
: #008;" class=3D"styled-by-prettify">struct</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> my_struct </span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"><br>&nbsp; &nbsp; </span><span style=3D"color: #8=
00;" class=3D"styled-by-prettify">// some members</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"><br>&nbsp; &nbsp; </span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">int</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> id</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br>&nbsp; &nbsp; </span><span style=3D"color: #800=
;" class=3D"styled-by-prettify">// more members</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>std</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify">list</span><span style=3D"color: #080;" class=3D"styled-by=
-prettify">&lt;my_struct&gt;</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> structs</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">(</span><span style=3D"color: #066;" class=3D"styled-by-pre=
ttify">100</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
);</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br>=
std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify">iota</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">make_member_iterator</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span styl=
e=3D"color: #008;" class=3D"styled-by-prettify">begin</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">(</span><font color=3D"#000000">=
<span style=3D"color: #000;" class=3D"styled-by-prettify">structs</span></f=
ont><span style=3D"color: #660;" class=3D"styled-by-prettify">),</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">&amp;</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify">my_struct</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify">id</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">),</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">{</span><span style=3D"color: #008;" class=3D"styled-by-prettify">e=
nd</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span>=
<font color=3D"#000000"><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify">structs</span></font><span style=3D"color: #660;" class=3D"styled-by=
-prettify">)},</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> </span><span style=3D"color: #066;" class=3D"styled-by-prettify">1</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">);</span></div=
></code></div><div><br></div><div>So you can do it without having to write =
a manual loop. You can also make things like std::find work without writing=
 a lambda</div><div><br></div><div><div class=3D"prettyprint" style=3D"back=
ground-color: rgb(250, 250, 250); border: 1px solid rgb(187, 187, 187); 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">find</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify">make_member_iterator</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #00=
8;" class=3D"styled-by-prettify">begin</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">(</span><font color=3D"#000000"><span style=3D"=
color: #000;" class=3D"styled-by-prettify">structs</span></font><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">&amp;</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify">my_struct</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">id</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">),</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">end</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">(</span><font color=3D"#0=
00000"><span style=3D"color: #000;" class=3D"styled-by-prettify">structs</s=
pan></font><span style=3D"color: #660;" class=3D"styled-by-prettify">)},</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><font=
 color=3D"#006666"><span style=3D"color: #066;" class=3D"styled-by-prettify=
">44</span></font><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>);</span></div></code></div><br>Both of which I think look better than the=
ir counterparts.</div><div><br></div><div>The implementation would be very =
simple (I can provide an example implementation if it's helpful but I doubt=
 I could catch the nuances of doing something like this) but I think it wou=
ld be helpful enough to warrant being included in the standard library.</di=
v></div></div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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_2708_15418323.1385945122880--

.


Author: Sean Middleditch <sean.middleditch@gmail.com>
Date: Sun, 1 Dec 2013 17:14:25 -0800 (PST)
Raw View
------=_Part_3262_2810138.1385946865130
Content-Type: text/plain; charset=ISO-8859-1

On Sunday, December 1, 2013 4:45:22 PM UTC-8, Sal Mast wrote:
>
> I don't follow proposals at all, and my C++ skills aren't so good, so many
> apologies if this is already easily possible or proposed, but would anyone
> else be interested in having an iterator adapter type that would iterate
> over the members of classes? The class would store another iterator (a real
> iterator from the container being iterated) and a pointer-to-member, and
> when it was dereferenced, it would dereference the iterator it is storing
> and apply the pointer-to-member and return a reference to it.
>

There is a reflection sub-committee looking into these types of things;
forum at
https://groups.google.com/a/isocpp.org/forum/?fromgroups#!forum/reflection.
 I think this issue is a lot more complex than you're thinking it might be;
consider a struct with members of two or more completely different types
and the problems with making a type-safe iterator that enumerates those.
 This is not something easily dealt with in C++ as it stands today, though
some proposals have come through with varies ideas at potential solutions.

--

---
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_3262_2810138.1385946865130
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Sunday, December 1, 2013 4:45:22 PM UTC-8, Sal Mast wro=
te:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;=
border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">I don't fo=
llow proposals at all, and my C++ skills aren't so good, so many apologies =
if this is already easily possible or proposed, but would anyone else be in=
terested in having an iterator adapter type that would iterate over the mem=
bers of classes? The class would store another iterator (a real iterator fr=
om the container being iterated) and a pointer-to-member, and when it was d=
ereferenced, it would dereference the iterator it is storing and apply the =
pointer-to-member and return a reference to it.</div></blockquote><div><br>=
</div><div>There is a reflection sub-committee looking into these types of =
things; forum at&nbsp;<a href=3D"https://groups.google.com/a/isocpp.org/for=
um/?fromgroups#!forum/reflection">https://groups.google.com/a/isocpp.org/fo=
rum/?fromgroups#!forum/reflection</a>. &nbsp;I think this issue is a lot mo=
re complex than you're thinking it might be; consider a struct with members=
 of two or more completely different types and the problems with making a t=
ype-safe iterator that enumerates those. &nbsp;This is not something easily=
 dealt with in C++ as it stands today, though some proposals have come thro=
ugh with varies ideas at potential solutions.</div></div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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_3262_2810138.1385946865130--

.


Author: "Billy O'Neal" <billy.oneal@gmail.com>
Date: Sun, 1 Dec 2013 17:17:29 -0800
Raw View
--089e0115eef8f59bd404ec82f38b
Content-Type: text/plain; charset=ISO-8859-1

@Sean: That's not what Sal asked for -- he's asking for an iterator type
that does a projection on the struct to one of its members. See his use
case passing a pointer to member data into a hypothetical iterator
generator function.

Billy O'Neal
https://github.com/BillyONeal/ <https://bitbucket.org/BillyONeal/>
http://stackoverflow.com/users/82320/billy-oneal
Malware Response Instructor - BleepingComputer.com


On Sun, Dec 1, 2013 at 5:14 PM, Sean Middleditch <sean.middleditch@gmail.com
> wrote:

> On Sunday, December 1, 2013 4:45:22 PM UTC-8, Sal Mast wrote:
>>
>> I don't follow proposals at all, and my C++ skills aren't so good, so
>> many apologies if this is already easily possible or proposed, but would
>> anyone else be interested in having an iterator adapter type that would
>> iterate over the members of classes? The class would store another iterator
>> (a real iterator from the container being iterated) and a
>> pointer-to-member, and when it was dereferenced, it would dereference the
>> iterator it is storing and apply the pointer-to-member and return a
>> reference to it.
>>
>
> There is a reflection sub-committee looking into these types of things;
> forum at
> https://groups.google.com/a/isocpp.org/forum/?fromgroups#!forum/reflection.
>  I think this issue is a lot more complex than you're thinking it might be;
> consider a struct with members of two or more completely different types
> and the problems with making a type-safe iterator that enumerates those.
>  This is not something easily dealt with in C++ as it stands today, though
> some proposals have come through with varies ideas at potential solutions.
>
> --
>
> ---
> 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/.

--089e0115eef8f59bd404ec82f38b
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">@Sean: That&#39;s not what Sal asked for -- he&#39;s askin=
g for an iterator type that does a projection on the struct to one of its m=
embers. See his use case passing a pointer to member data into a hypothetic=
al iterator generator function.</div>

<div class=3D"gmail_extra"><br clear=3D"all"><div><div dir=3D"ltr"><div>Bil=
ly O&#39;Neal</div><div><a href=3D"https://bitbucket.org/BillyONeal/" targe=
t=3D"_blank">https://github.com/BillyONeal/</a></div><div><a href=3D"http:/=
/stackoverflow.com/users/82320/billy-oneal" target=3D"_blank">http://stacko=
verflow.com/users/82320/billy-oneal</a></div>

<div>Malware Response Instructor - BleepingComputer.com</div></div></div>
<br><br><div class=3D"gmail_quote">On Sun, Dec 1, 2013 at 5:14 PM, Sean Mid=
dleditch <span dir=3D"ltr">&lt;<a href=3D"mailto:sean.middleditch@gmail.com=
" target=3D"_blank">sean.middleditch@gmail.com</a>&gt;</span> wrote:<br><bl=
ockquote 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"im">On Sunday, December 1, 2013 4:45:22 PM U=
TC-8, Sal Mast wrote:<blockquote class=3D"gmail_quote" style=3D"margin:0;ma=
rgin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"lt=
r">I don&#39;t follow proposals at all, and my C++ skills aren&#39;t so goo=
d, so many apologies if this is already easily possible or proposed, but wo=
uld anyone else be interested in having an iterator adapter type that would=
 iterate over the members of classes? The class would store another iterato=
r (a real iterator from the container being iterated) and a pointer-to-memb=
er, and when it was dereferenced, it would dereference the iterator it is s=
toring and apply the pointer-to-member and return a reference to it.</div>

</blockquote><div><br></div></div><div>There is a reflection sub-committee =
looking into these types of things; forum at=A0<a href=3D"https://groups.go=
ogle.com/a/isocpp.org/forum/?fromgroups#!forum/reflection" target=3D"_blank=
">https://groups.google.com/a/isocpp.org/forum/?fromgroups#!forum/reflectio=
n</a>. =A0I think this issue is a lot more complex than you&#39;re thinking=
 it might be; consider a struct with members of two or more completely diff=
erent types and the problems with making a type-safe iterator that enumerat=
es those. =A0This is not something easily dealt with in C++ as it stands to=
day, though some proposals have come through with varies ideas at potential=
 solutions.</div>

</div><div class=3D"HOEnZb"><div class=3D"h5">

<p></p>

-- <br>
=A0<br>
--- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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>
</div></div></blockquote></div><br></div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />

--089e0115eef8f59bd404ec82f38b--

.


Author: Peter Sommerlad <peter.sommerlad@hsr.ch>
Date: Mon, 2 Dec 2013 11:14:31 +0100
Raw View
Take a look at Boost's iterator adapters, i.e., make_transform_iterator if =
you can construct an iterator for your needs, i.e. by using std::function<i=
nt&(mystruct &)>{&mystruct::id}

Peter.
On 02.12.2013, at 01:45, Sal Mast <salmast303@gmail.com> wrote:

> I don't follow proposals at all, and my C++ skills aren't so good, so man=
y apologies if this is already easily possible or proposed, but would anyon=
e else be interested in having an iterator adapter type that would iterate =
over the members of classes? The class would store another iterator (a real=
 iterator from the container being iterated) and a pointer-to-member, and w=
hen it was dereferenced, it would dereference the iterator it is storing an=
d apply the pointer-to-member and return a reference to it.
>=20
> I often come upon circumstances where I want to apply algorithms on the d=
ata members of types in a container, but I have to write the algorithm manu=
ally or write a lambda. Again, this could be due to my poor C++ skills and =
there could be a simple solution that I am unaware of, but if not, I think =
a member iterator adapter would allow better utilization of standard algori=
thms. An example of the use of it would be:
>=20
> struct my_struct {
>     // some members
>     int id;
>     // more members
> };
>=20
> std::list<my_struct> structs(100);
>=20
> std::iota(make_member_iterator(begin(structs), &my_struct::id), {end(stru=
cts)}, 1);
>=20
> So you can do it without having to write a manual loop. You can also make=
 things like std::find work without writing a lambda
>=20
> std::find(make_member_iterator(begin(structs), &my_struct::id), {end(stru=
cts)}, 44);
>=20
> Both of which I think look better than their counterparts.
>=20
> The implementation would be very simple (I can provide an example impleme=
ntation if it's helpful but I doubt I could catch the nuances of doing some=
thing like this) but I think it would be helpful enough to warrant being in=
cluded in the standard library.
>=20
> --=20
> =20
> ---=20
> 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-propo=
sals/.

--=20
Prof. Peter Sommerlad

Institut f=FCr Software: Bessere Software - Einfach, Schneller!
HSR Hochschule f=FCr Technik Rapperswil
Oberseestr 10, Postfach 1475, CH-8640 Rapperswil

http://ifs.hsr.ch http://cute-test.com http://linticator.com http://includa=
tor.com
tel:+41 55 222 49 84 =3D=3D mobile:+41 79 432 23 32
fax:+41 55 222 46 29 =3D=3D mailto:peter.sommerlad@hsr.ch





--=20

---=20
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 e=
mail 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-proposa=
ls/.

.