Topic: Using a method... of a data member
Author: Vincent Reverdy <vince.rev@gmail.com>
Date: Sat, 19 Apr 2014 17:47:47 -0700 (PDT)
Raw View
------=_Part_2707_17047275.1397954867500
Content-Type: text/plain; charset=UTF-8
Hello,
I often face a situation where it seems more logical to use composition
instead of inheritance. It may not be the best example, but let's say:
template <class T>
class myvector
{
private: std::vector<T> v;
};
A problem arises when we want to expose some basic functionalities of the
inner vector: we start to write a lot of code just to forward arguments to
the inner data member.
One trick to avoid that is to use a CRTP base class, that will generate all
the forwarding code for each of my classes that will contain a std::vector.
But it is not very simple to implement for C++ beginners.
I think that it would be very practical to have a syntax like:
template <class T>
class myvector
{
private: std::vector<T> v;
public: using v.push_back; // Generates a forwarding push_back function
to v
};
which will generate code that just do forwarding. In this example, a call
to myvector::push_back will simply forward the arguments to v and return
the result (void in this situation). I know that one can achieve the same
kind of thing with inheritance, but sometimes composition is preferable.
I do not have a sufficient background to say whether it may cause a lot of
troubles in the language, but naively I would see that syntax as a very
useful tool.
Any comments or remarks?
Thanks,
Vincent
--
---
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_2707_17047275.1397954867500
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hello, <br><br>I often face a situation where it seems mor=
e logical to use composition instead of inheritance. It may not be the best=
example, but let's say:<br><br><span style=3D"font-family: courier new,mon=
ospace;">template <class T><br>class myvector<br>{<br> &nb=
sp; private: std::vector<T> v;<br>};<br></span> <br>A problem ar=
ises when we want to expose some basic functionalities of the inner vector:=
we start to write a lot of code just to forward arguments to the inner dat=
a member.<br>One trick to avoid that is to use a CRTP base class, that will=
generate all the forwarding code for each of my classes that will contain =
a <span style=3D"font-family: courier new,monospace;">std::vector</span>. B=
ut it is not very simple to implement for C++ beginners.<br>I think that it=
would be very practical to have a syntax like:<br><br><span style=3D"font-=
family: courier new,monospace;">template <class T><br>class myvector<=
br>{<br> private: std::vector<T> v;<br> =
public: using v.push_back; // Generates a forwarding push_back funct=
ion to v<br>};</span><br><br>which will generate code that just do forwardi=
ng. In this example, a call to <span style=3D"font-family: courier new,mono=
space;">myvector::push_back</span> will simply forward the arguments to <sp=
an style=3D"font-family: courier new,monospace;">v</span> and return the re=
sult (void in this situation). I know that one can achieve the same kind of=
thing with inheritance, but sometimes composition is preferable.<br><br>I =
do not have a sufficient background to say whether it may cause a lot of tr=
oubles in the language, but naively I would see that syntax as a very usefu=
l tool.<br><br>Any comments or remarks?<br><br>Thanks, <br><br>Vincent<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_2707_17047275.1397954867500--
.
Author: David Krauss <potswa@gmail.com>
Date: Sun, 20 Apr 2014 09:40:09 +0800
Raw View
--Apple-Mail=_E390A937-3DB3-4134-B3CD-1F3F3F372A47
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1
On 2014-04-20, at 8:47 AM, Vincent Reverdy <vince.rev@gmail.com> wrote:
> public: using v.push_back; // Generates a forwarding push_back functi=
on to v
This could probably be tacked on to my recent inline variable proposal. v.p=
ush_back is a member access expression with well-defined category and type:
> Otherwise, if E1.E2 refers to a non-static member function and the type o=
f E2 is "function of parameter-type-list cv ref-qualifieropt returning T", =
then E1.E2 is a prvalue. The expression designates a non-static member func=
tion. The expression can be used only as the left-hand operand of a member =
function call (9.3). [ Note: Any redundant set of parentheses surrounding t=
he expression is ignored (5.1). --endnote] The type of E1.E2 is "function o=
f parameter-type-list cv returning T".=20
>=20
To be practical, it would also require inline auto nonstatic members. Such =
a feature already came up in the discussion on this list, for nonstatic dat=
a members, but this sort of nonstatic member function is a natural extrapol=
ation.
They are probably best left to a follow-on proposal once the basic version =
gains some approval. At least, that's my intuition.
Experienced proposers: Is it better to be more ambitious on a first draft, =
then cut back for approval, and then re-propose something with some familia=
rity?
--=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/.
--Apple-Mail=_E390A937-3DB3-4134-B3CD-1F3F3F372A47
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=ISO-8859-1
<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dwindows-1252"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-=
mode: space; -webkit-line-break: after-white-space;"><br><div><div>On 2014&=
ndash;04–20, at 8:47 AM, Vincent Reverdy <<a href=3D"mailto:vince.=
rev@gmail.com">vince.rev@gmail.com</a>> wrote:</div><br class=3D"Apple-i=
nterchange-newline"><blockquote type=3D"cite"><div dir=3D"ltr"><span style=
=3D"font-family: courier new,monospace;"> public: using v.push=
_back; // Generates a forwarding push_back function to v<br></span></div></=
blockquote><div><br></div><div>This could probably be tacked on to my recen=
t inline variable proposal. <font face=3D"Courier">v.push_back</font> is a =
member access expression with well-defined category and type:</div><div><bl=
ockquote type=3D"cite">
=09
=09
=09
=09
=09
<div class=3D"page" title=3D"Page 115">
<div class=3D"layoutArea">
<div class=3D"column"><p><span style=3D"font-size: 10.000000pt; font-fa=
mily: 'LMRoman10'">Otherwise, if </span><span style=3D"font-size: 10.000000=
pt; font-family: 'LMMono10'">E1.E2 </span><span style=3D"font-size: 10.0000=
00pt; font-family: 'LMRoman10'">refers to a non-static member function and =
the type of </span><span style=3D"font-size: 10.000000pt; font-family: 'LMM=
ono10'">E2 </span><span style=3D"font-size: 10.000000pt; font-family: 'LMRo=
man10'">is “function of
parameter-type-list </span><span style=3D"font-size: 10.000000pt; font-fami=
ly: 'LMRoman10'; font-style: italic">cv ref-qualifier</span><span style=3D"=
font-size: 7.000000pt; font-family: 'LMRoman7'; font-style: italic; vertica=
l-align: -1.000000pt">opt </span><span style=3D"font-size: 10.000000pt; fon=
t-family: 'LMRoman10'">returning </span><span style=3D"font-size: 10.000000=
pt; font-family: 'LMMono10'">T</span><span style=3D"font-size: 10.000000pt;=
font-family: 'LMRoman10'">”, then </span><span style=3D"font-size: 1=
0.000000pt; font-family: 'LMMono10'">E1.E2 </span><span style=3D"font-size:=
10.000000pt; font-family: 'LMRoman10'">is a prvalue. The expression
designates a non-static member function. The expression can be used only as=
the left-hand
operand of a member function call (</span><span style=3D"font-size: 10.0000=
00pt; font-family: 'LMRoman10'; color: rgb(0.000000%, 0.000000%, 100.000000=
%)">9.3</span><span style=3D"font-size: 10.000000pt; font-family: 'LMRoman1=
0'">). [ </span><span style=3D"font-size: 10.000000pt; font-family: 'LMRoma=
n10'; font-style: italic">Note: </span><span style=3D"font-size: 10.000000p=
t; font-family: 'LMRoman10'">Any redundant set of parentheses surrounding
the expression is ignored (</span><span style=3D"font-size: 10.000000pt; fo=
nt-family: 'LMRoman10'; color: rgb(0.000000%, 0.000000%, 100.000000%)">5.1<=
/span><span style=3D"font-size: 10.000000pt; font-family: 'LMRoman10'">). <=
/span><span style=3D"font-size: 10.000000pt; font-family: 'LMRoman10'; font=
-style: italic">—endnote</span><span style=3D"font-size: 10.000000pt;=
font-family: 'LMRoman10'">] The type of </span><span style=3D"font-si=
ze: 10.000000pt; font-family: 'LMMono10'">E1.E2 </span><span style=3D"=
font-size: 10.000000pt; font-family: 'LMRoman10'">is “function of par=
ameter-type-list
</span><span style=3D"font-size: 10.000000pt; font-family: 'LMRoman10'; fon=
t-style: italic">cv </span><span style=3D"font-size: 10.000000pt; font-fami=
ly: 'LMRoman10'">returning </span><span style=3D"font-size: 10.000000pt; fo=
nt-family: 'LMMono10'">T</span><span style=3D"font-size: 10.000000pt; font-=
family: 'LMRoman10'">”. </span></p>
</div>
</div>
</div></blockquote></div><div>To be practical, it would also require <fon=
t face=3D"Courier">inline auto</font> nonstatic members. Such a feature alr=
eady came up in the discussion on this list, for nonstatic data members, bu=
t this sort of nonstatic member function is a natural extrapolation.</div><=
div><br></div><div>They are probably best left to a follow-on proposal once=
the basic version gains some approval. At least, that’s my intuition=
..</div></div><div><br></div><div>Experienced proposers: Is it better to be =
more ambitious on a first draft, then cut back for approval, and then re-pr=
opose something with some familiarity?</div><div><br></div></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 />
--Apple-Mail=_E390A937-3DB3-4134-B3CD-1F3F3F372A47--
.