Topic: Lambda "Injection/Inheritance/Override
Author: "Arthur O'Dwyer" <arthur.j.odwyer@gmail.com>
Date: Tue, 18 Jul 2017 11:37:58 -0700
Raw View
--94eb2c0e76fc8697ea05549bd01b
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
On Mon, Jul 17, 2017 at 4:08 PM, 'Michael Davies' via ISO C++ Standard -
Future Proposals <std-proposals@isocpp.org> wrote:
> Hi Arthur,
>
> Thanks for the response! In regards to this, I was thinking more along th=
e
> lines of treating the lambda function as if you were actually defining a
> function definition within the class. So:
>
> [] () {
> m_iWin +=3D1;
> std::cout << "Overloaded: " << m_iWin << '\n';
> }
>
> Would be more equivalent to treating the lambda function as if it were a
> direct function definition:
>
> TestOverride::winProcess(){
> m_iWin +=3D 1;
> std::cout << "Overloaded: " << m_iWin << '\n';
> }
>
> Perhaps there should be some way of defining the lambda as if it were
> actually a function that was part of the class? Rather than those data
> members being accessible by main() they are only accessible within the ve=
ry
> lambda you create rather than accessible by main().
>
Remember, private members are not accessible to anyone except actual class
members (and friends). Private members simply cannot be accessed by anyone
who doesn't have permission to access them; that's the whole point of
"accessibility" controls. If lambdas were allowed to break this rule, then
every member of a class might as well be public. Example:
class Lockbox {
private:
int m_secrets =3D 42;
};
int main() {
Lockbox box;
int secrets =3D [](Lockbox& b) { return b.m_secrets; }(box);
assert(secrets =3D=3D 42);
}
What you can do within the language, without breaking the rules, is make
main() a *friend* of class Lockbox.
Or, you could change m_secrets to be *public*, in which case the above code
would work fine.
Or, you could change m_secrets to be *protected*, and then derive from
Lockbox (as long as it's not final); then you could get at the secrets of a
WeakLockbox you created yourself, or with only a *slight modicum* of
undefined behavior, get at the secrets of an arbitrary Lockbox. Example:
https://wandbox.org/permlink/y7cF6NrwmjAyc08w
class Lockbox {
protected:
int m_secrets =3D 42;
};
struct WeakLockbox : Lockbox {
int& get_secrets() { return m_secrets; }
// technically undefined behavior if you're acting on a Lockbox that
*isn't* Weak
static int& get_secrets(Lockbox& b) {
return ((WeakLockbox&)b).m_secrets;
}
};
IMHO, if you're doing this for purposes of unit-testing, you almost
certainly should be using *friend* declarations; that's one of the very few
cases where friend declarations are more useful than confusing.
class Lockbox {
private:
int m_secrets =3D 42;
friend class TestOverride;
};
=E2=80=93Arthur
--=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.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/CADvuK0%2By7O58oKBV7ZEBJ_Vj%3D%3DqFx8inzUdBy3jnc=
n5Uj4Enfg%40mail.gmail.com.
--94eb2c0e76fc8697ea05549bd01b
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Mon, Jul 17, 2017 at 4:08 PM, 'Michael Davies' =
via ISO C++ Standard - Future Proposals <span dir=3D"ltr"><<a href=3D"ma=
ilto:std-proposals@isocpp.org" target=3D"_blank">std-proposals@isocpp.org</=
a>></span> wrote:<br><div class=3D"gmail_extra"><div class=3D"gmail_quot=
e"><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bord=
er-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:soli=
d;padding-left:1ex"><div dir=3D"ltr">Hi Arthur,<div><br></div><div>Thanks f=
or the response! In regards to this, I was thinking more along the lines of=
treating the lambda function as if you were actually defining a function d=
efinition within the class. So:<br><br><div class=3D"gmail-m_-2254069233616=
394319prettyprint" style=3D"background-color:rgb(250,250,250);border:1px so=
lid rgb(187,187,187);word-wrap:break-word"><code class=3D"gmail-m_-22540692=
33616394319prettyprint"><div class=3D"gmail-m_-2254069233616394319subpretty=
print"><span style=3D"color:rgb(102,102,0)" class=3D"gmail-m_-2254069233616=
394319styled-by-prettify">[]</span><span style=3D"color:rgb(0,0,0)" class=
=3D"gmail-m_-2254069233616394319styled-by-prettify"> </span><span style=3D"=
color:rgb(102,102,0)" class=3D"gmail-m_-2254069233616394319styled-by-pretti=
fy">()</span><span style=3D"color:rgb(0,0,0)" class=3D"gmail-m_-22540692336=
16394319styled-by-prettify"> </span><span style=3D"color:rgb(102,102,0)" cl=
ass=3D"gmail-m_-2254069233616394319styled-by-prettify">{</span><span style=
=3D"color:rgb(0,0,0)" class=3D"gmail-m_-2254069233616394319styled-by-pretti=
fy"> <br>m_iWin </span><span style=3D"color:rgb(102,102,0)" class=3D"gmail-=
m_-2254069233616394319styled-by-prettify">+=3D</span><span style=3D"color:r=
gb(0,102,102)" class=3D"gmail-m_-2254069233616394319styled-by-prettify">1</=
span><span style=3D"color:rgb(102,102,0)" class=3D"gmail-m_-225406923361639=
4319styled-by-prettify">;</span><span style=3D"color:rgb(0,0,0)" class=3D"g=
mail-m_-2254069233616394319styled-by-prettify"><br>std</span><span style=3D=
"color:rgb(102,102,0)" class=3D"gmail-m_-2254069233616394319styled-by-prett=
ify">::</span><span style=3D"color:rgb(0,0,0)" class=3D"gmail-m_-2254069233=
616394319styled-by-prettify">cout </span><font color=3D"#666600"><span styl=
e=3D"color:rgb(102,102,0)" class=3D"gmail-m_-2254069233616394319styled-by-p=
rettify"><<</span><span style=3D"color:rgb(0,0,0)" class=3D"gmail-m_-=
2254069233616394319styled-by-prettify"> </span><span style=3D"color:rgb(0,1=
36,0)" class=3D"gmail-m_-2254069233616394319styled-by-prettify">"Overl=
oaded: "</span><span style=3D"color:rgb(0,0,0)" class=3D"gmail-m_-2254=
069233616394319styled-by-prettify"> </span><span style=3D"color:rgb(102,102=
,0)" class=3D"gmail-m_-2254069233616394319styled-by-prettify"><<</spa=
n><span style=3D"color:rgb(0,0,0)" class=3D"gmail-m_-2254069233616394319sty=
led-by-prettify"> m_iWin </span><span style=3D"color:rgb(102,102,0)" class=
=3D"gmail-m_-2254069233616394319styled-by-prettify"><<</span><span st=
yle=3D"color:rgb(0,0,0)" class=3D"gmail-m_-2254069233616394319styled-by-pre=
ttify"> </span><span style=3D"color:rgb(0,136,0)" class=3D"gmail-m_-2254069=
233616394319styled-by-prettify">'\n'</span><span style=3D"color:rgb=
(102,102,0)" class=3D"gmail-m_-2254069233616394319styled-by-prettify">;</sp=
an></font><span style=3D"color:rgb(0,0,0)" class=3D"gmail-m_-22540692336163=
94319styled-by-prettify"><br></span><span style=3D"color:rgb(102,102,0)" cl=
ass=3D"gmail-m_-2254069233616394319styled-by-prettify">}</span></div></code=
></div><br>Would be more equivalent to treating the lambda=C2=A0function as=
if it were a direct function definition:</div><div><br></div><div><div cla=
ss=3D"gmail-m_-2254069233616394319prettyprint" style=3D"background-color:rg=
b(250,250,250);border:1px solid rgb(187,187,187);word-wrap:break-word"><cod=
e class=3D"gmail-m_-2254069233616394319prettyprint"><div class=3D"gmail-m_-=
2254069233616394319subprettyprint"><span style=3D"color:rgb(102,0,102)"><sp=
an style=3D"color:rgb(102,0,102)" class=3D"gmail-m_-2254069233616394319styl=
ed-by-prettify">TestOverride</span><span style=3D"color:rgb(102,102,0)" cla=
ss=3D"gmail-m_-2254069233616394319styled-by-prettify">::</span><span style=
=3D"color:rgb(0,0,0)" class=3D"gmail-m_-2254069233616394319styled-by-pretti=
fy">winProcess</span><span style=3D"color:rgb(102,102,0)" class=3D"gmail-m_=
-2254069233616394319styled-by-prettify">(){</span><span style=3D"color:rgb(=
0,0,0)" class=3D"gmail-m_-2254069233616394319styled-by-prettify"><br>=C2=A0=
=C2=A0 m_iWin </span><span style=3D"color:rgb(102,102,0)" class=3D"gmail-m=
_-2254069233616394319styled-by-prettify">+=3D</span><span style=3D"color:rg=
b(0,0,0)" class=3D"gmail-m_-2254069233616394319styled-by-prettify"> </span>=
<span style=3D"color:rgb(0,102,102)" class=3D"gmail-m_-2254069233616394319s=
tyled-by-prettify">1</span><span style=3D"color:rgb(102,102,0)" class=3D"gm=
ail-m_-2254069233616394319styled-by-prettify">;</span><span style=3D"color:=
rgb(0,0,0)" class=3D"gmail-m_-2254069233616394319styled-by-prettify"><br>=
=C2=A0 =C2=A0 std</span><span style=3D"color:rgb(102,102,0)" class=3D"gmail=
-m_-2254069233616394319styled-by-prettify">::</span><span style=3D"color:rg=
b(0,0,0)" class=3D"gmail-m_-2254069233616394319styled-by-prettify">cout </s=
pan><span style=3D"color:rgb(102,102,0)" class=3D"gmail-m_-2254069233616394=
319styled-by-prettify"><<</span><span style=3D"color:rgb(0,0,0)" clas=
s=3D"gmail-m_-2254069233616394319styled-by-prettify"> </span><span style=3D=
"color:rgb(0,136,0)" class=3D"gmail-m_-2254069233616394319styled-by-prettif=
y">"Overloaded: "</span><span style=3D"color:rgb(0,0,0)" class=3D=
"gmail-m_-2254069233616394319styled-by-prettify"> </span><span style=3D"col=
or:rgb(102,102,0)" class=3D"gmail-m_-2254069233616394319styled-by-prettify"=
><<</span><span style=3D"color:rgb(0,0,0)" class=3D"gmail-m_-22540692=
33616394319styled-by-prettify"> m_iWin </span><span style=3D"color:rgb(102,=
102,0)" class=3D"gmail-m_-2254069233616394319styled-by-prettify"><<</=
span><span style=3D"color:rgb(0,0,0)" class=3D"gmail-m_-2254069233616394319=
styled-by-prettify"> </span><span style=3D"color:rgb(0,136,0)" class=3D"gma=
il-m_-2254069233616394319styled-by-prettify">'\n'</span><span style=
=3D"color:rgb(102,102,0)" class=3D"gmail-m_-2254069233616394319styled-by-pr=
ettify">;</span><span style=3D"color:rgb(0,0,0)" class=3D"gmail-m_-22540692=
33616394319styled-by-prettify"><br></span><span style=3D"color:rgb(102,102,=
0)" class=3D"gmail-m_-2254069233616394319styled-by-prettify">}</span></span=
><span style=3D"color:rgb(0,0,0)" class=3D"gmail-m_-2254069233616394319styl=
ed-by-prettify"><br></span></div></code></div><br>Perhaps there should be s=
ome way of defining the lambda as if it were actually a function that was p=
art of the class? Rather than those data members being accessible by main()=
they are only accessible within the very lambda you create rather than acc=
essible by main().<br></div></div></blockquote><div><br></div><div>Remember=
, private members are not accessible to anyone except actual class members =
(and friends). Private members simply cannot be accessed by anyone who does=
n't have permission to access them; that's the whole point of "=
;accessibility" controls. If lambdas were allowed to break this rule, =
then every member of a class might as well be public. Example:</div><div><b=
r></div><div><font face=3D"monospace, monospace">class Lockbox {</font></di=
v><div><font face=3D"monospace, monospace">private:</font></div><div><font =
face=3D"monospace, monospace">=C2=A0 =C2=A0 int m_secrets =3D 42;</font></d=
iv><div><font face=3D"monospace, monospace">};<br></font></div><div><font f=
ace=3D"monospace, monospace"><br></font></div><div><font face=3D"monospace,=
monospace">int main() {</font></div><div><font face=3D"monospace, monospac=
e">=C2=A0 =C2=A0 Lockbox box;</font></div><div><font face=3D"monospace, mon=
ospace">=C2=A0 =C2=A0 int secrets =3D [](Lockbox& b) { return b.m_secre=
ts; }(box);</font></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=
=A0 assert(secrets =3D=3D 42);</font></div><div><font face=3D"monospace, mo=
nospace">}</font></div><div><br></div><div>What you can do within the langu=
age, without breaking the rules, is make main() a <b>friend</b> of class Lo=
ckbox.</div><div>Or, you could change m_secrets to be <b>public</b>, in whi=
ch case the above code would work fine.</div><div>Or, you could change m_se=
crets to be <b>protected</b>, and then derive from Lockbox (as long as it&#=
39;s not final); then you could get at the secrets of a WeakLockbox you cre=
ated yourself, or with only a <i>slight modicum</i> of undefined behavior, =
get at the secrets of an arbitrary Lockbox. Example:</div><div><a href=3D"h=
ttps://wandbox.org/permlink/y7cF6NrwmjAyc08w">https://wandbox.org/permlink/=
y7cF6NrwmjAyc08w</a><br></div><div><br></div><div><div><div><font face=3D"m=
onospace, monospace">class Lockbox {</font></div><div><font face=3D"monospa=
ce, monospace">protected:</font></div><div><font face=3D"monospace, monospa=
ce">=C2=A0 =C2=A0 int m_secrets =3D 42;</font></div><div><font face=3D"mono=
space, monospace">};</font></div><div><font face=3D"monospace, monospace"><=
br></font></div><div><font face=3D"monospace, monospace">struct WeakLockbox=
: Lockbox {</font></div><div><font face=3D"monospace, monospace">=C2=A0 =
=C2=A0 int& get_secrets() { return m_secrets; }</font></div><div><font =
face=3D"monospace, monospace"><br></font></div><div><font face=3D"monospace=
, monospace">=C2=A0 =C2=A0 // technically undefined behavior if you're =
acting on a Lockbox that <i>isn't</i> Weak</font></div><div><font face=
=3D"monospace, monospace">=C2=A0 =C2=A0 static int& get_secrets(Lockbox=
& b) {</font></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 return ((WeakLockbox&)b).m_secrets;</font></div><div>=
<font face=3D"monospace, monospace">=C2=A0 =C2=A0 }</font></div><div><font =
face=3D"monospace, monospace">};</font></div></div><div><br></div></div><di=
v>IMHO, if you're doing this for purposes of unit-testing, you almost c=
ertainly should be using <b>friend</b> declarations; that's one of the =
very few cases where friend declarations are more useful than confusing.</d=
iv><div><br></div><div><font face=3D"monospace, monospace">class Lockbox {<=
/font></div><div><font face=3D"monospace, monospace">private:</font></div><=
div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 int m_secrets =3D 42;=
</font></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 friend =
class TestOverride;</font></div><div><font face=3D"monospace, monospace">};=
</font></div><div><br></div><div>=E2=80=93Arthur</div></div></div></div>
<p></p>
-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CADvuK0%2By7O58oKBV7ZEBJ_Vj%3D%3DqFx8=
inzUdBy3jncn5Uj4Enfg%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfoote=
r">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CADvuK0%2By=
7O58oKBV7ZEBJ_Vj%3D%3DqFx8inzUdBy3jncn5Uj4Enfg%40mail.gmail.com</a>.<br />
--94eb2c0e76fc8697ea05549bd01b--
.