Topic: generic dynamic / runtime cast (cast to real


Author: Thiago Macieira <thiago@macieira.org>
Date: Sun, 16 Oct 2016 10:22:26 -0700
Raw View
Em domingo, 16 de outubro de 2016, =C3=A0s 09:10:59 PDT, 'David Feurle' via=
 ISO C++=20
Standard - Future Proposals escreveu:
> Hello,
>=20
> I like the works of C# dynamic which casts a variable to it's real runtim=
e
> type and I would like to see a similar feature in C++.

You're describing something that C++ has already had since the very first=
=20
version: virtual functions.

> Example in C#:
>=20
> Object o =3D new String("hahaha");
> dynamic d =3D o;                                    // the type of d is n=
ow
> string
> d +=3D "haha";                                        //append something =
to
> the string
>=20
> As you can see the dynamic variable will be of the runtime type of it's
> content. The following code uses the operators of string.

In C++, we already have that without the need for a special keyword.

 Object *o =3D new DerivedObject("hahaha");
 *o +=3D "haha";

Works just fine, provided that operator+=3D is a virtual function in Object=
..

> The code that uses the dynamic variable could be compiled  since all
> candidates are known at compiletime.

Say what?=20

> Rtti could be used to determine the type at runtime.

It already can be. In the example I gave above:

 Object *o =3D new DerivedObject("hahaha");

We can assert:
 &typeid(*o) =3D=3D &typeid(DerivedObject)

> Example in C++:
>=20
> struct Base {
>     virtual Base() =3D default;

You meant ~Base. You also need:

 virtual void fun() =3D 0;

> };
>=20
> struct A : public Base { void fun(); };
> struct B : public Base { void fun(); };
>=20
> fun(A*);
> fun(B*);
>=20
> Base* b =3D new A();
> auto dyn =3D runtime_cast<A*, B*>(b);   // the type of dyn is either A* o=
r B*
> if (!dyn) { return; }                                 //the type of b was
> neither A* or B*
> dyn->fun();                                           // either A::fun is
> called or B::fun
>                                                              //all possib=
le

Why do we need this? With the pure virtual like I added, you can write:

 Base *b =3D new A;
 dyn->fun();

> What do you think about this?

I think it's unnecessary to change what we already have.

--=20
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center

--=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/4908423.ESY8U5GBX0%40tjmaciei-mobl1.

.


Author: "'David Feurle' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Sun, 16 Oct 2016 10:44:01 -0700 (PDT)
Raw View
------=_Part_1458_478646409.1476639841248
Content-Type: multipart/alternative;
 boundary="----=_Part_1459_952957022.1476639841249"

------=_Part_1459_952957022.1476639841249
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hello Thiago,=20

there are things that can not be done with virtual functions. For example d=
ouble=20
dispatch=20
<https://en.wikipedia.org/wiki/Double_dispatch#Double_dispatch_is_more_than=
_function_overloading> and=20
the visitor <https://en.wikipedia.org/wiki/Visitor_pattern#C.2B.2B_example>=
..

It is possible to simulate a dynamic call functionality with virtual=20
functions. But it is a hazzle and several classes / functions need to be=20
implemented to add this functionality.

Imagine the following "standard" example for double dispatch:

class SpaceShip {};class ApolloSpacecraft : public SpaceShip {};
class Asteroid {public:
  virtual void CollideWith(SpaceShip&) {
    cout << "Asteroid hit a SpaceShip" << endl;
  }
  virtual void CollideWith(ApolloSpacecraft&) {
    cout << "Asteroid hit an ApolloSpacecraft" << endl;
  }};
class ExplodingAsteroid : public Asteroid {public:
  virtual void CollideWith(SpaceShip&) {
    cout << "ExplodingAsteroid hit a SpaceShip" << endl;
  }
  virtual void CollideWith(ApolloSpacecraft&) {
    cout << "ExplodingAsteroid hit an ApolloSpacecraft" << endl;
  }};


SpaceShip& theSpaceShipReference =3D theApolloSpacecraft;theAsteroid.Collid=
eWith(theSpaceShipReference);theAsteroidReference.CollideWith(theSpaceShipR=
eference);

=20
This will not call the correct function even though we have a overload for=
=20
it. Currently we need to employ a visitor / double dispatch to all classes.=
=20
In C# it is part of the language.=20
The visitor pattern is often used and it would not be neccessary to=20
implement it if we would have a runtime cast. Read more on the motivation=
=20
section for visitor on wikipedia=20
<https://en.wikipedia.org/wiki/Visitor_pattern#Motivation>
=20

> Em domingo, 16 de outubro de 2016, =C3=A0s 09:10:59 PDT, 'David Feurle' v=
ia ISO=20
> C++=20
> Standard - Future Proposals escreveu:=20
> > Hello,=20
> >=20
> > I like the works of C# dynamic which casts a variable to it's real=20
> runtime=20
> > type and I would like to see a similar feature in C++.=20
>
> You're describing something that C++ has already had since the very first=
=20
> version: virtual functions.=20
>
> > Example in C#:=20
> >=20
> > Object o =3D new String("hahaha");=20
> > dynamic d =3D o;                                    // the type of d is=
=20
> now=20
> > string=20
> > d +=3D "haha";                                        //append somethin=
g=20
> to=20
> > the string=20
> >=20
> > As you can see the dynamic variable will be of the runtime type of it's=
=20
> > content. The following code uses the operators of string.=20
>
> In C++, we already have that without the need for a special keyword.=20
>
>         Object *o =3D new DerivedObject("hahaha");=20
>         *o +=3D "haha";=20
>
> Works just fine, provided that operator+=3D is a virtual function in Obje=
ct.=20
>
> > The code that uses the dynamic variable could be compiled  since all=20
> > candidates are known at compiletime.=20
>
> Say what?=20
>
> > Rtti could be used to determine the type at runtime.=20
>
> It already can be. In the example I gave above:=20
>
>         Object *o =3D new DerivedObject("hahaha");=20
>
> We can assert:=20
>         &typeid(*o) =3D=3D &typeid(DerivedObject)=20
>
> > Example in C++:=20
> >=20
> > struct Base {=20
> >     virtual Base() =3D default;=20
>
> You meant ~Base. You also need:=20
>
>         virtual void fun() =3D 0;=20
>
> > };=20
> >=20
> > struct A : public Base { void fun(); };=20
> > struct B : public Base { void fun(); };=20
> >=20
> > fun(A*);=20
> > fun(B*);=20
> >=20
> > Base* b =3D new A();=20
> > auto dyn =3D runtime_cast<A*, B*>(b);   // the type of dyn is either A*=
 or=20
> B*=20
> > if (!dyn) { return; }                                 //the type of b=
=20
> was=20
> > neither A* or B*=20
> > dyn->fun();                                           // either A::fun=
=20
> is=20
> > called or B::fun=20
> >                                                              //all=20
> possible=20
>
> Why do we need this? With the pure virtual like I added, you can write:=
=20
>
>         Base *b =3D new A;=20
>         dyn->fun();=20
>
> > What do you think about this?=20
>
> I think it's unnecessary to change what we already have.=20
>
> --=20
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org=20
>    Software Architect - Intel Open Source Technology Center=20
>
>
Am Sonntag, 16. Oktober 2016 19:22:31 UTC+2 schrieb Thiago Macieira:
>
> Em domingo, 16 de outubro de 2016, =C3=A0s 09:10:59 PDT, 'David Feurle' v=
ia ISO=20
> C++=20
> Standard - Future Proposals escreveu:=20
> > Hello,=20
> >=20
> > I like the works of C# dynamic which casts a variable to it's real=20
> runtime=20
> > type and I would like to see a similar feature in C++.=20
>
> You're describing something that C++ has already had since the very first=
=20
> version: virtual functions.=20
>
> > Example in C#:=20
> >=20
> > Object o =3D new String("hahaha");=20
> > dynamic d =3D o;                                    // the type of d is=
=20
> now=20
> > string=20
> > d +=3D "haha";                                        //append somethin=
g=20
> to=20
> > the string=20
> >=20
> > As you can see the dynamic variable will be of the runtime type of it's=
=20
> > content. The following code uses the operators of string.=20
>
> In C++, we already have that without the need for a special keyword.=20
>
>         Object *o =3D new DerivedObject("hahaha");=20
>         *o +=3D "haha";=20
>
> Works just fine, provided that operator+=3D is a virtual function in Obje=
ct.=20
>
> > The code that uses the dynamic variable could be compiled  since all=20
> > candidates are known at compiletime.=20
>
> Say what?=20
>
> > Rtti could be used to determine the type at runtime.=20
>
> It already can be. In the example I gave above:=20
>
>         Object *o =3D new DerivedObject("hahaha");=20
>
> We can assert:=20
>         &typeid(*o) =3D=3D &typeid(DerivedObject)=20
>
> > Example in C++:=20
> >=20
> > struct Base {=20
> >     virtual Base() =3D default;=20
>
> You meant ~Base. You also need:=20
>
>         virtual void fun() =3D 0;=20
>
> > };=20
> >=20
> > struct A : public Base { void fun(); };=20
> > struct B : public Base { void fun(); };=20
> >=20
> > fun(A*);=20
> > fun(B*);=20
> >=20
> > Base* b =3D new A();=20
> > auto dyn =3D runtime_cast<A*, B*>(b);   // the type of dyn is either A*=
 or=20
> B*=20
> > if (!dyn) { return; }                                 //the type of b=
=20
> was=20
> > neither A* or B*=20
> > dyn->fun();                                           // either A::fun=
=20
> is=20
> > called or B::fun=20
> >                                                              //all=20
> possible=20
>
> Why do we need this? With the pure virtual like I added, you can write:=
=20
>
>         Base *b =3D new A;=20
>         dyn->fun();=20
>
> > What do you think about this?=20
>
> I think it's unnecessary to change what we already have.=20
>
> --=20
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org=20
>    Software Architect - Intel Open Source Technology Center=20
>
>
Am Sonntag, 16. Oktober 2016 19:22:31 UTC+2 schrieb Thiago Macieira:
>
> Em domingo, 16 de outubro de 2016, =C3=A0s 09:10:59 PDT, 'David Feurle' v=
ia ISO=20
> C++=20
> Standard - Future Proposals escreveu:=20
> > Hello,=20
> >=20
> > I like the works of C# dynamic which casts a variable to it's real=20
> runtime=20
> > type and I would like to see a similar feature in C++.=20
>
> You're describing something that C++ has already had since the very first=
=20
> version: virtual functions.=20
>
> > Example in C#:=20
> >=20
> > Object o =3D new String("hahaha");=20
> > dynamic d =3D o;                                    // the type of d is=
=20
> now=20
> > string=20
> > d +=3D "haha";                                        //append somethin=
g=20
> to=20
> > the string=20
> >=20
> > As you can see the dynamic variable will be of the runtime type of it's=
=20
> > content. The following code uses the operators of string.=20
>
> In C++, we already have that without the need for a special keyword.=20
>
>         Object *o =3D new DerivedObject("hahaha");=20
>         *o +=3D "haha";=20
>
> Works just fine, provided that operator+=3D is a virtual function in Obje=
ct.=20
>
> > The code that uses the dynamic variable could be compiled  since all=20
> > candidates are known at compiletime.=20
>
> Say what?=20
>
> > Rtti could be used to determine the type at runtime.=20
>
> It already can be. In the example I gave above:=20
>
>         Object *o =3D new DerivedObject("hahaha");=20
>
> We can assert:=20
>         &typeid(*o) =3D=3D &typeid(DerivedObject)=20
>
> > Example in C++:=20
> >=20
> > struct Base {=20
> >     virtual Base() =3D default;=20
>
> You meant ~Base. You also need:=20
>
>         virtual void fun() =3D 0;=20
>
> > };=20
> >=20
> > struct A : public Base { void fun(); };=20
> > struct B : public Base { void fun(); };=20
> >=20
> > fun(A*);=20
> > fun(B*);=20
> >=20
> > Base* b =3D new A();=20
> > auto dyn =3D runtime_cast<A*, B*>(b);   // the type of dyn is either A*=
 or=20
> B*=20
> > if (!dyn) { return; }                                 //the type of b=
=20
> was=20
> > neither A* or B*=20
> > dyn->fun();                                           // either A::fun=
=20
> is=20
> > called or B::fun=20
> >                                                              //all=20
> possible=20
>
> Why do we need this? With the pure virtual like I added, you can write:=
=20
>
>         Base *b =3D new A;=20
>         dyn->fun();=20
>
> > What do you think about this?=20
>
> I think it's unnecessary to change what we already have.=20
>
> --=20
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org=20
>    Software Architect - Intel Open Source Technology Center=20
>
>
Am Sonntag, 16. Oktober 2016 19:22:31 UTC+2 schrieb Thiago Macieira:
>
> Em domingo, 16 de outubro de 2016, =C3=A0s 09:10:59 PDT, 'David Feurle' v=
ia ISO=20
> C++=20
> Standard - Future Proposals escreveu:=20
> > Hello,=20
> >=20
> > I like the works of C# dynamic which casts a variable to it's real=20
> runtime=20
> > type and I would like to see a similar feature in C++.=20
>
> You're describing something that C++ has already had since the very first=
=20
> version: virtual functions.=20
>
> > Example in C#:=20
> >=20
> > Object o =3D new String("hahaha");=20
> > dynamic d =3D o;                                    // the type of d is=
=20
> now=20
> > string=20
> > d +=3D "haha";                                        //append somethin=
g=20
> to=20
> > the string=20
> >=20
> > As you can see the dynamic variable will be of the runtime type of it's=
=20
> > content. The following code uses the operators of string.=20
>
> In C++, we already have that without the need for a special keyword.=20
>
>         Object *o =3D new DerivedObject("hahaha");=20
>         *o +=3D "haha";=20
>
> Works just fine, provided that operator+=3D is a virtual function in Obje=
ct.=20
>
> > The code that uses the dynamic variable could be compiled  since all=20
> > candidates are known at compiletime.=20
>
> Say what?=20
>
> > Rtti could be used to determine the type at runtime.=20
>
> It already can be. In the example I gave above:=20
>
>         Object *o =3D new DerivedObject("hahaha");=20
>
> We can assert:=20
>         &typeid(*o) =3D=3D &typeid(DerivedObject)=20
>
> > Example in C++:=20
> >=20
> > struct Base {=20
> >     virtual Base() =3D default;=20
>
> You meant ~Base. You also need:=20
>
>         virtual void fun() =3D 0;=20
>
> > };=20
> >=20
> > struct A : public Base { void fun(); };=20
> > struct B : public Base { void fun(); };=20
> >=20
> > fun(A*);=20
> > fun(B*);=20
> >=20
> > Base* b =3D new A();=20
> > auto dyn =3D runtime_cast<A*, B*>(b);   // the type of dyn is either A*=
 or=20
> B*=20
> > if (!dyn) { return; }                                 //the type of b=
=20
> was=20
> > neither A* or B*=20
> > dyn->fun();                                           // either A::fun=
=20
> is=20
> > called or B::fun=20
> >                                                              //all=20
> possible=20
>
> Why do we need this? With the pure virtual like I added, you can write:=
=20
>
>         Base *b =3D new A;=20
>         dyn->fun();=20
>
> > What do you think about this?=20
>
> I think it's unnecessary to change what we already have.=20
>
> --=20
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org=20
>    Software Architect - Intel Open Source Technology Center=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.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/39dc6003-567c-4506-9fbb-2e0340313960%40isocpp.or=
g.

------=_Part_1459_952957022.1476639841249
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hello Thiago,=C2=A0<div><br></div><div>there are things th=
at can not be done with virtual functions. For example=C2=A0<a href=3D"http=
s://en.wikipedia.org/wiki/Double_dispatch#Double_dispatch_is_more_than_func=
tion_overloading">double dispatch</a>=C2=A0and the=C2=A0<a href=3D"https://=
en.wikipedia.org/wiki/Visitor_pattern#C.2B.2B_example">visitor</a>.</div><d=
iv><br></div><div>It is possible to simulate a dynamic call functionality w=
ith virtual functions. But it is a hazzle and several classes / functions n=
eed to be implemented to add this functionality.</div><div><br></div><div>I=
magine the following &quot;standard&quot; example for double dispatch:</div=
><div><br></div><div><pre style=3D"font-family: monospace, Courier; color: =
rgb(0, 0, 0); background-color: rgb(249, 249, 249); border-width: 1px; bord=
er-style: solid; border-color: rgb(221, 221, 221); padding: 1em; white-spac=
e: pre-wrap; line-height: 1.3em; tab-size: 4; font-size: 14px;"><span class=
=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">class</span> <sp=
an class=3D"nc" style=3D"color: rgb(0, 0, 255); font-weight: bold;">SpaceSh=
ip</span> <span class=3D"p">{};</span>
<span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">class=
</span> <span class=3D"nc" style=3D"color: rgb(0, 0, 255); font-weight: bol=
d;">ApolloSpacecraft</span> <span class=3D"o" style=3D"color: rgb(102, 102,=
 102);">:</span> <span class=3D"k" style=3D"color: rgb(0, 128, 0); font-wei=
ght: bold;">public</span> <span class=3D"n">SpaceShip</span> <span class=3D=
"p">{};</span>

<span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">class=
</span> <span class=3D"nc" style=3D"color: rgb(0, 0, 255); font-weight: bol=
d;">Asteroid</span> <span class=3D"p">{</span>
<span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">publi=
c</span><span class=3D"o" style=3D"color: rgb(102, 102, 102);">:</span>
  <span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">vir=
tual</span> <span class=3D"kt" style=3D"color: rgb(176, 0, 64);">void</span=
> <span class=3D"n">CollideWith</span><span class=3D"p">(</span><span class=
=3D"n">SpaceShip</span><span class=3D"o" style=3D"color: rgb(102, 102, 102)=
;">&amp;</span><span class=3D"p">)</span> <span class=3D"p">{</span>
    <span class=3D"n">cout</span> <span class=3D"o" style=3D"color: rgb(102=
, 102, 102);">&lt;&lt;</span> <span class=3D"s" style=3D"color: rgb(186, 33=
, 33);">&quot;Asteroid hit a SpaceShip&quot;</span> <span class=3D"o" style=
=3D"color: rgb(102, 102, 102);">&lt;&lt;</span> <span class=3D"n">endl</spa=
n><span class=3D"p">;</span>
  <span class=3D"p">}</span>
  <span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">vir=
tual</span> <span class=3D"kt" style=3D"color: rgb(176, 0, 64);">void</span=
> <span class=3D"n">CollideWith</span><span class=3D"p">(</span><span class=
=3D"n">ApolloSpacecraft</span><span class=3D"o" style=3D"color: rgb(102, 10=
2, 102);">&amp;</span><span class=3D"p">)</span> <span class=3D"p">{</span>
    <span class=3D"n">cout</span> <span class=3D"o" style=3D"color: rgb(102=
, 102, 102);">&lt;&lt;</span> <span class=3D"s" style=3D"color: rgb(186, 33=
, 33);">&quot;Asteroid hit an ApolloSpacecraft&quot;</span> <span class=3D"=
o" style=3D"color: rgb(102, 102, 102);">&lt;&lt;</span> <span class=3D"n">e=
ndl</span><span class=3D"p">;</span>
  <span class=3D"p">}</span>
<span class=3D"p">};</span>

<span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">class=
</span> <span class=3D"nc" style=3D"color: rgb(0, 0, 255); font-weight: bol=
d;">ExplodingAsteroid</span> <span class=3D"o" style=3D"color: rgb(102, 102=
, 102);">:</span> <span class=3D"k" style=3D"color: rgb(0, 128, 0); font-we=
ight: bold;">public</span> <span class=3D"n">Asteroid</span> <span class=3D=
"p">{</span>
<span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">publi=
c</span><span class=3D"o" style=3D"color: rgb(102, 102, 102);">:</span>
  <span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">vir=
tual</span> <span class=3D"kt" style=3D"color: rgb(176, 0, 64);">void</span=
> <span class=3D"n">CollideWith</span><span class=3D"p">(</span><span class=
=3D"n">SpaceShip</span><span class=3D"o" style=3D"color: rgb(102, 102, 102)=
;">&amp;</span><span class=3D"p">)</span> <span class=3D"p">{</span>
    <span class=3D"n">cout</span> <span class=3D"o" style=3D"color: rgb(102=
, 102, 102);">&lt;&lt;</span> <span class=3D"s" style=3D"color: rgb(186, 33=
, 33);">&quot;ExplodingAsteroid hit a SpaceShip&quot;</span> <span class=3D=
"o" style=3D"color: rgb(102, 102, 102);">&lt;&lt;</span> <span class=3D"n">=
endl</span><span class=3D"p">;</span>
  <span class=3D"p">}</span>
  <span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">vir=
tual</span> <span class=3D"kt" style=3D"color: rgb(176, 0, 64);">void</span=
> <span class=3D"n">CollideWith</span><span class=3D"p">(</span><span class=
=3D"n">ApolloSpacecraft</span><span class=3D"o" style=3D"color: rgb(102, 10=
2, 102);">&amp;</span><span class=3D"p">)</span> <span class=3D"p">{</span>
    <span class=3D"n">cout</span> <span class=3D"o" style=3D"color: rgb(102=
, 102, 102);">&lt;&lt;</span> <span class=3D"s" style=3D"color: rgb(186, 33=
, 33);">&quot;ExplodingAsteroid hit an ApolloSpacecraft&quot;</span> <span =
class=3D"o" style=3D"color: rgb(102, 102, 102);">&lt;&lt;</span> <span clas=
s=3D"n">endl</span><span class=3D"p">;</span>
  <span class=3D"p">}</span>
<span class=3D"p">};</span></pre></div><div><br></div><div><pre style=3D"fo=
nt-family: monospace, Courier; color: rgb(0, 0, 0); background-color: rgb(2=
49, 249, 249); border-width: 1px; border-style: solid; border-color: rgb(22=
1, 221, 221); padding: 1em; white-space: pre-wrap; line-height: 1.3em; tab-=
size: 4; font-size: 14px;"><span class=3D"n">SpaceShip</span><span class=3D=
"o" style=3D"color: rgb(102, 102, 102);">&amp;</span> <span class=3D"n">the=
SpaceShipReference</span> <span class=3D"o" style=3D"color: rgb(102, 102, 1=
02);">=3D</span> <span class=3D"n">theApolloSpacecraft</span><span class=3D=
"p">;</span>
<span class=3D"n">theAsteroid</span><span class=3D"p">.</span><span class=
=3D"n">CollideWith</span><span class=3D"p">(</span><span class=3D"n">theSpa=
ceShipReference</span><span class=3D"p">);</span>
<span class=3D"n">theAsteroidReference</span><span class=3D"p">.</span><spa=
n class=3D"n">CollideWith</span><span class=3D"p">(</span><span class=3D"n"=
>theSpaceShipReference</span><span class=3D"p">);</span></pre><div>=C2=A0</=
div><div>This will not call the correct function even though we have a over=
load for it. Currently we need to employ a visitor / double dispatch to all=
 classes. In C# it is part of the language.=C2=A0</div><div>The visitor pat=
tern is often used and it would not be neccessary to implement it if we wou=
ld have a runtime cast. Read more on the motivation section for visitor on=
=C2=A0<a href=3D"https://en.wikipedia.org/wiki/Visitor_pattern#Motivation">=
wikipedia</a></div><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=
=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: =
1ex;">Em domingo, 16 de outubro de 2016, =C3=A0s 09:10:59 PDT, &#39;David F=
eurle&#39; via ISO C++=20
<br>Standard - Future Proposals escreveu:
<br>&gt; Hello,
<br>&gt;=20
<br>&gt; I like the works of C# dynamic which casts a variable to it&#39;s =
real runtime
<br>&gt; type and I would like to see a similar feature in C++.
<br>
<br>You&#39;re describing something that C++ has already had since the very=
 first=20
<br>version: virtual functions.
<br>
<br>&gt; Example in C#:
<br>&gt;=20
<br>&gt; Object o =3D new String(&quot;hahaha&quot;);
<br>&gt; dynamic d =3D o; =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// the type of d is now
<br>&gt; string
<br>&gt; d +=3D &quot;haha&quot;; =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 =C2=A0//append something to
<br>&gt; the string
<br>&gt;=20
<br>&gt; As you can see the dynamic variable will be of the runtime type of=
 it&#39;s
<br>&gt; content. The following code uses the operators of string.
<br>
<br>In C++, we already have that without the need for a special keyword.
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Object *o =3D new Deriv=
edObject(&quot;hahaha&quot;);
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*o +=3D &quot;haha&quot=
;;
<br>
<br>Works just fine, provided that operator+=3D is a virtual function in Ob=
ject.
<br>
<br>&gt; The code that uses the dynamic variable could be compiled =C2=A0si=
nce all
<br>&gt; candidates are known at compiletime.
<br>
<br>Say what?=20
<br>
<br>&gt; Rtti could be used to determine the type at runtime.
<br>
<br>It already can be. In the example I gave above:
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Object *o =3D new Deriv=
edObject(&quot;hahaha&quot;);
<br>
<br>We can assert:
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0&amp;typeid(*o) =3D=3D =
&amp;typeid(DerivedObject)
<br>
<br>&gt; Example in C++:
<br>&gt;=20
<br>&gt; struct Base {
<br>&gt; =C2=A0 =C2=A0 virtual Base() =3D default;
<br>
<br>You meant ~Base. You also need:
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0virtual void fun() =3D =
0;
<br>
<br>&gt; };
<br>&gt;=20
<br>&gt; struct A : public Base { void fun(); };
<br>&gt; struct B : public Base { void fun(); };
<br>&gt;=20
<br>&gt; fun(A*);
<br>&gt; fun(B*);
<br>&gt;=20
<br>&gt; Base* b =3D new A();
<br>&gt; auto dyn =3D runtime_cast&lt;A*, B*&gt;(b); =C2=A0 // the type of =
dyn is either A* or B*
<br>&gt; if (!dyn) { return; } =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 //the=
 type of b was
<br>&gt; neither A* or B*
<br>&gt; dyn-&gt;fun(); =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 =C2=A0 =C2=A0 // either A::fun is
<br>&gt; called or B::fun
<br>&gt; =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 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0//all possible
<br>
<br>Why do we need this? With the pure virtual like I added, you can write:
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Base *b =3D new A;
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0dyn-&gt;fun();
<br>
<br>&gt; What do you think about this?
<br>
<br>I think it&#39;s unnecessary to change what we already have.
<br>
<br>--=20
<br>Thiago Macieira - thiago (AT) <a href=3D"http://macieira.info" target=
=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;http://www.goo=
gle.com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x3dD\x26sntz\x3d1\x26usg\=
x3dAFQjCNEswDUBNCNanbu7euhqLn_62FW8ag&#39;;return true;" onclick=3D"this.hr=
ef=3D&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x=
3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEswDUBNCNanbu7euhqLn_62FW8ag&#39;;return t=
rue;">macieira.info</a> - thiago (AT) <a href=3D"http://kde.org" target=3D"=
_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;http://www.google.=
com/url?q\x3dhttp%3A%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH=
GRJdo5_JYG1DowztwAHAKs80XSA&#39;;return true;" onclick=3D"this.href=3D&#39;=
http://www.google.com/url?q\x3dhttp%3A%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1=
\x26usg\x3dAFQjCNHGRJdo5_JYG1DowztwAHAKs80XSA&#39;;return true;">kde.org</a=
>
<br>=C2=A0 =C2=A0Software Architect - Intel Open Source Technology Center
<br>
<br></blockquote><br>Am Sonntag, 16. Oktober 2016 19:22:31 UTC+2 schrieb Th=
iago Macieira:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Em domingo, 16 d=
e outubro de 2016, =C3=A0s 09:10:59 PDT, &#39;David Feurle&#39; via ISO C++=
=20
<br>Standard - Future Proposals escreveu:
<br>&gt; Hello,
<br>&gt;=20
<br>&gt; I like the works of C# dynamic which casts a variable to it&#39;s =
real runtime
<br>&gt; type and I would like to see a similar feature in C++.
<br>
<br>You&#39;re describing something that C++ has already had since the very=
 first=20
<br>version: virtual functions.
<br>
<br>&gt; Example in C#:
<br>&gt;=20
<br>&gt; Object o =3D new String(&quot;hahaha&quot;);
<br>&gt; dynamic d =3D o; =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// the type of d is now
<br>&gt; string
<br>&gt; d +=3D &quot;haha&quot;; =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 =C2=A0//append something to
<br>&gt; the string
<br>&gt;=20
<br>&gt; As you can see the dynamic variable will be of the runtime type of=
 it&#39;s
<br>&gt; content. The following code uses the operators of string.
<br>
<br>In C++, we already have that without the need for a special keyword.
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Object *o =3D new Deriv=
edObject(&quot;hahaha&quot;);
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*o +=3D &quot;haha&quot=
;;
<br>
<br>Works just fine, provided that operator+=3D is a virtual function in Ob=
ject.
<br>
<br>&gt; The code that uses the dynamic variable could be compiled =C2=A0si=
nce all
<br>&gt; candidates are known at compiletime.
<br>
<br>Say what?=20
<br>
<br>&gt; Rtti could be used to determine the type at runtime.
<br>
<br>It already can be. In the example I gave above:
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Object *o =3D new Deriv=
edObject(&quot;hahaha&quot;);
<br>
<br>We can assert:
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0&amp;typeid(*o) =3D=3D =
&amp;typeid(DerivedObject)
<br>
<br>&gt; Example in C++:
<br>&gt;=20
<br>&gt; struct Base {
<br>&gt; =C2=A0 =C2=A0 virtual Base() =3D default;
<br>
<br>You meant ~Base. You also need:
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0virtual void fun() =3D =
0;
<br>
<br>&gt; };
<br>&gt;=20
<br>&gt; struct A : public Base { void fun(); };
<br>&gt; struct B : public Base { void fun(); };
<br>&gt;=20
<br>&gt; fun(A*);
<br>&gt; fun(B*);
<br>&gt;=20
<br>&gt; Base* b =3D new A();
<br>&gt; auto dyn =3D runtime_cast&lt;A*, B*&gt;(b); =C2=A0 // the type of =
dyn is either A* or B*
<br>&gt; if (!dyn) { return; } =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 //the=
 type of b was
<br>&gt; neither A* or B*
<br>&gt; dyn-&gt;fun(); =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 =C2=A0 =C2=A0 // either A::fun is
<br>&gt; called or B::fun
<br>&gt; =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 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0//all possible
<br>
<br>Why do we need this? With the pure virtual like I added, you can write:
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Base *b =3D new A;
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0dyn-&gt;fun();
<br>
<br>&gt; What do you think about this?
<br>
<br>I think it&#39;s unnecessary to change what we already have.
<br>
<br>--=20
<br>Thiago Macieira - thiago (AT) <a href=3D"http://macieira.info" target=
=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;http://www.goo=
gle.com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x3dD\x26sntz\x3d1\x26usg\=
x3dAFQjCNEswDUBNCNanbu7euhqLn_62FW8ag&#39;;return true;" onclick=3D"this.hr=
ef=3D&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x=
3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEswDUBNCNanbu7euhqLn_62FW8ag&#39;;return t=
rue;">macieira.info</a> - thiago (AT) <a href=3D"http://kde.org" target=3D"=
_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;http://www.google.=
com/url?q\x3dhttp%3A%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH=
GRJdo5_JYG1DowztwAHAKs80XSA&#39;;return true;" onclick=3D"this.href=3D&#39;=
http://www.google.com/url?q\x3dhttp%3A%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1=
\x26usg\x3dAFQjCNHGRJdo5_JYG1DowztwAHAKs80XSA&#39;;return true;">kde.org</a=
>
<br>=C2=A0 =C2=A0Software Architect - Intel Open Source Technology Center
<br>
<br></blockquote><br>Am Sonntag, 16. Oktober 2016 19:22:31 UTC+2 schrieb Th=
iago Macieira:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Em domingo, 16 d=
e outubro de 2016, =C3=A0s 09:10:59 PDT, &#39;David Feurle&#39; via ISO C++=
=20
<br>Standard - Future Proposals escreveu:
<br>&gt; Hello,
<br>&gt;=20
<br>&gt; I like the works of C# dynamic which casts a variable to it&#39;s =
real runtime
<br>&gt; type and I would like to see a similar feature in C++.
<br>
<br>You&#39;re describing something that C++ has already had since the very=
 first=20
<br>version: virtual functions.
<br>
<br>&gt; Example in C#:
<br>&gt;=20
<br>&gt; Object o =3D new String(&quot;hahaha&quot;);
<br>&gt; dynamic d =3D o; =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// the type of d is now
<br>&gt; string
<br>&gt; d +=3D &quot;haha&quot;; =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 =C2=A0//append something to
<br>&gt; the string
<br>&gt;=20
<br>&gt; As you can see the dynamic variable will be of the runtime type of=
 it&#39;s
<br>&gt; content. The following code uses the operators of string.
<br>
<br>In C++, we already have that without the need for a special keyword.
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Object *o =3D new Deriv=
edObject(&quot;hahaha&quot;);
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*o +=3D &quot;haha&quot=
;;
<br>
<br>Works just fine, provided that operator+=3D is a virtual function in Ob=
ject.
<br>
<br>&gt; The code that uses the dynamic variable could be compiled =C2=A0si=
nce all
<br>&gt; candidates are known at compiletime.
<br>
<br>Say what?=20
<br>
<br>&gt; Rtti could be used to determine the type at runtime.
<br>
<br>It already can be. In the example I gave above:
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Object *o =3D new Deriv=
edObject(&quot;hahaha&quot;);
<br>
<br>We can assert:
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0&amp;typeid(*o) =3D=3D =
&amp;typeid(DerivedObject)
<br>
<br>&gt; Example in C++:
<br>&gt;=20
<br>&gt; struct Base {
<br>&gt; =C2=A0 =C2=A0 virtual Base() =3D default;
<br>
<br>You meant ~Base. You also need:
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0virtual void fun() =3D =
0;
<br>
<br>&gt; };
<br>&gt;=20
<br>&gt; struct A : public Base { void fun(); };
<br>&gt; struct B : public Base { void fun(); };
<br>&gt;=20
<br>&gt; fun(A*);
<br>&gt; fun(B*);
<br>&gt;=20
<br>&gt; Base* b =3D new A();
<br>&gt; auto dyn =3D runtime_cast&lt;A*, B*&gt;(b); =C2=A0 // the type of =
dyn is either A* or B*
<br>&gt; if (!dyn) { return; } =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 //the=
 type of b was
<br>&gt; neither A* or B*
<br>&gt; dyn-&gt;fun(); =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 =C2=A0 =C2=A0 // either A::fun is
<br>&gt; called or B::fun
<br>&gt; =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 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0//all possible
<br>
<br>Why do we need this? With the pure virtual like I added, you can write:
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Base *b =3D new A;
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0dyn-&gt;fun();
<br>
<br>&gt; What do you think about this?
<br>
<br>I think it&#39;s unnecessary to change what we already have.
<br>
<br>--=20
<br>Thiago Macieira - thiago (AT) <a href=3D"http://macieira.info" target=
=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;http://www.goo=
gle.com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x3dD\x26sntz\x3d1\x26usg\=
x3dAFQjCNEswDUBNCNanbu7euhqLn_62FW8ag&#39;;return true;" onclick=3D"this.hr=
ef=3D&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x=
3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEswDUBNCNanbu7euhqLn_62FW8ag&#39;;return t=
rue;">macieira.info</a> - thiago (AT) <a href=3D"http://kde.org" target=3D"=
_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;http://www.google.=
com/url?q\x3dhttp%3A%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH=
GRJdo5_JYG1DowztwAHAKs80XSA&#39;;return true;" onclick=3D"this.href=3D&#39;=
http://www.google.com/url?q\x3dhttp%3A%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1=
\x26usg\x3dAFQjCNHGRJdo5_JYG1DowztwAHAKs80XSA&#39;;return true;">kde.org</a=
>
<br>=C2=A0 =C2=A0Software Architect - Intel Open Source Technology Center
<br>
<br></blockquote><br>Am Sonntag, 16. Oktober 2016 19:22:31 UTC+2 schrieb Th=
iago Macieira:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Em domingo, 16 d=
e outubro de 2016, =C3=A0s 09:10:59 PDT, &#39;David Feurle&#39; via ISO C++=
=20
<br>Standard - Future Proposals escreveu:
<br>&gt; Hello,
<br>&gt;=20
<br>&gt; I like the works of C# dynamic which casts a variable to it&#39;s =
real runtime
<br>&gt; type and I would like to see a similar feature in C++.
<br>
<br>You&#39;re describing something that C++ has already had since the very=
 first=20
<br>version: virtual functions.
<br>
<br>&gt; Example in C#:
<br>&gt;=20
<br>&gt; Object o =3D new String(&quot;hahaha&quot;);
<br>&gt; dynamic d =3D o; =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// the type of d is now
<br>&gt; string
<br>&gt; d +=3D &quot;haha&quot;; =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 =C2=A0//append something to
<br>&gt; the string
<br>&gt;=20
<br>&gt; As you can see the dynamic variable will be of the runtime type of=
 it&#39;s
<br>&gt; content. The following code uses the operators of string.
<br>
<br>In C++, we already have that without the need for a special keyword.
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Object *o =3D new Deriv=
edObject(&quot;hahaha&quot;);
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*o +=3D &quot;haha&quot=
;;
<br>
<br>Works just fine, provided that operator+=3D is a virtual function in Ob=
ject.
<br>
<br>&gt; The code that uses the dynamic variable could be compiled =C2=A0si=
nce all
<br>&gt; candidates are known at compiletime.
<br>
<br>Say what?=20
<br>
<br>&gt; Rtti could be used to determine the type at runtime.
<br>
<br>It already can be. In the example I gave above:
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Object *o =3D new Deriv=
edObject(&quot;hahaha&quot;);
<br>
<br>We can assert:
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0&amp;typeid(*o) =3D=3D =
&amp;typeid(DerivedObject)
<br>
<br>&gt; Example in C++:
<br>&gt;=20
<br>&gt; struct Base {
<br>&gt; =C2=A0 =C2=A0 virtual Base() =3D default;
<br>
<br>You meant ~Base. You also need:
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0virtual void fun() =3D =
0;
<br>
<br>&gt; };
<br>&gt;=20
<br>&gt; struct A : public Base { void fun(); };
<br>&gt; struct B : public Base { void fun(); };
<br>&gt;=20
<br>&gt; fun(A*);
<br>&gt; fun(B*);
<br>&gt;=20
<br>&gt; Base* b =3D new A();
<br>&gt; auto dyn =3D runtime_cast&lt;A*, B*&gt;(b); =C2=A0 // the type of =
dyn is either A* or B*
<br>&gt; if (!dyn) { return; } =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 //the=
 type of b was
<br>&gt; neither A* or B*
<br>&gt; dyn-&gt;fun(); =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 =C2=A0 =C2=A0 // either A::fun is
<br>&gt; called or B::fun
<br>&gt; =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 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0//all possible
<br>
<br>Why do we need this? With the pure virtual like I added, you can write:
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Base *b =3D new A;
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0dyn-&gt;fun();
<br>
<br>&gt; What do you think about this?
<br>
<br>I think it&#39;s unnecessary to change what we already have.
<br>
<br>--=20
<br>Thiago Macieira - thiago (AT) <a href=3D"http://macieira.info" target=
=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;http://www.goo=
gle.com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x3dD\x26sntz\x3d1\x26usg\=
x3dAFQjCNEswDUBNCNanbu7euhqLn_62FW8ag&#39;;return true;" onclick=3D"this.hr=
ef=3D&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x=
3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEswDUBNCNanbu7euhqLn_62FW8ag&#39;;return t=
rue;">macieira.info</a> - thiago (AT) <a href=3D"http://kde.org" target=3D"=
_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;http://www.google.=
com/url?q\x3dhttp%3A%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH=
GRJdo5_JYG1DowztwAHAKs80XSA&#39;;return true;" onclick=3D"this.href=3D&#39;=
http://www.google.com/url?q\x3dhttp%3A%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1=
\x26usg\x3dAFQjCNHGRJdo5_JYG1DowztwAHAKs80XSA&#39;;return true;">kde.org</a=
>
<br>=C2=A0 =C2=A0Software Architect - Intel Open Source Technology Center
<br>
<br></blockquote></div></div>

<p></p>

-- <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+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/39dc6003-567c-4506-9fbb-2e0340313960%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/39dc6003-567c-4506-9fbb-2e0340313960=
%40isocpp.org</a>.<br />

------=_Part_1459_952957022.1476639841249--

------=_Part_1458_478646409.1476639841248--

.


Author: Tony V E <tvaneerd@gmail.com>
Date: Sun, 16 Oct 2016 13:57:06 -0400
Raw View
<html><head></head><body lang=3D"en-US" style=3D"background-color: rgb(255,=
 255, 255); line-height: initial;">                                        =
                                              <div style=3D"width: 100%; fo=
nt-size: initial; font-family: Calibri, 'Slate Pro', sans-serif, sans-serif=
; color: rgb(31, 73, 125); text-align: initial; background-color: rgb(255, =
255, 255);">There are some pattern matching proposals that might be related=
 here. </div>                                                              =
                                                                       <div=
 style=3D"width: 100%; font-size: initial; font-family: Calibri, 'Slate Pro=
', sans-serif, sans-serif; color: rgb(31, 73, 125); text-align: initial; ba=
ckground-color: rgb(255, 255, 255);"><br style=3D"display:initial"></div>  =
                                                                           =
                                                                           =
                                           <div style=3D"font-size: initial=
; font-family: Calibri, 'Slate Pro', sans-serif, sans-serif; color: rgb(31,=
 73, 125); text-align: initial; background-color: rgb(255, 255, 255);">Sent=
&nbsp;from&nbsp;my&nbsp;BlackBerry&nbsp;portable&nbsp;Babbage&nbsp;Device</=
div>                                                                       =
                                                                           =
                                <table width=3D"100%" style=3D"background-c=
olor:white;border-spacing:0px;"> <tbody><tr><td colspan=3D"2" style=3D"font=
-size: initial; text-align: initial; background-color: rgb(255, 255, 255);"=
>                           <div style=3D"border-style: solid none none; bo=
rder-top-color: rgb(181, 196, 223); border-top-width: 1pt; padding: 3pt 0in=
 0in; font-family: Tahoma, 'BB Alpha Sans', 'Slate Pro'; font-size: 10pt;">=
  <div><b>From: </b>'David Feurle' via ISO C++ Standard - Future Proposals<=
/div><div><b>Sent: </b>Sunday, October 16, 2016 1:44 PM</div><div><b>To: </=
b>ISO C++ Standard - Future Proposals</div><div><b>Reply To: </b>std-propos=
als@isocpp.org</div><div><b>Subject: </b>Re: [std-proposals] generic dynami=
c / runtime cast (cast to real runtime type)</div></div></td></tr></tbody><=
/table><div style=3D"border-style: solid none none; border-top-color: rgb(1=
86, 188, 209); border-top-width: 1pt; font-size: initial; text-align: initi=
al; background-color: rgb(255, 255, 255);"></div><br><div id=3D"_originalCo=
ntent" style=3D""><div dir=3D"ltr">Hello Thiago,&nbsp;<div><br></div><div>t=
here are things that can not be done with virtual functions. For example&nb=
sp;<a href=3D"https://en.wikipedia.org/wiki/Double_dispatch#Double_dispatch=
_is_more_than_function_overloading">double dispatch</a>&nbsp;and the&nbsp;<=
a href=3D"https://en.wikipedia.org/wiki/Visitor_pattern#C.2B.2B_example">vi=
sitor</a>.</div><div><br></div><div>It is possible to simulate a dynamic ca=
ll functionality with virtual functions. But it is a hazzle and several cla=
sses / functions need to be implemented to add this functionality.</div><di=
v><br></div><div>Imagine the following "standard" example for double dispat=
ch:</div><div><br></div><div><pre style=3D"font-family: monospace, Courier;=
 color: rgb(0, 0, 0); background-color: rgb(249, 249, 249); border-width: 1=
px; border-style: solid; border-color: rgb(221, 221, 221); padding: 1em; wh=
ite-space: pre-wrap; line-height: 1.3em; tab-size: 4; font-size: 14px;"><sp=
an class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">class</s=
pan> <span class=3D"nc" style=3D"color: rgb(0, 0, 255); font-weight: bold;"=
>SpaceShip</span> <span class=3D"p">{};</span>
<span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">class=
</span> <span class=3D"nc" style=3D"color: rgb(0, 0, 255); font-weight: bol=
d;">ApolloSpacecraft</span> <span class=3D"o" style=3D"color: rgb(102, 102,=
 102);">:</span> <span class=3D"k" style=3D"color: rgb(0, 128, 0); font-wei=
ght: bold;">public</span> <span class=3D"n">SpaceShip</span> <span class=3D=
"p">{};</span>

<span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">class=
</span> <span class=3D"nc" style=3D"color: rgb(0, 0, 255); font-weight: bol=
d;">Asteroid</span> <span class=3D"p">{</span>
<span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">publi=
c</span><span class=3D"o" style=3D"color: rgb(102, 102, 102);">:</span>
  <span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">vir=
tual</span> <span class=3D"kt" style=3D"color: rgb(176, 0, 64);">void</span=
> <span class=3D"n">CollideWith</span><span class=3D"p">(</span><span class=
=3D"n">SpaceShip</span><span class=3D"o" style=3D"color: rgb(102, 102, 102)=
;">&amp;</span><span class=3D"p">)</span> <span class=3D"p">{</span>
    <span class=3D"n">cout</span> <span class=3D"o" style=3D"color: rgb(102=
, 102, 102);">&lt;&lt;</span> <span class=3D"s" style=3D"color: rgb(186, 33=
, 33);">"Asteroid hit a SpaceShip"</span> <span class=3D"o" style=3D"color:=
 rgb(102, 102, 102);">&lt;&lt;</span> <span class=3D"n">endl</span><span cl=
ass=3D"p">;</span>
  <span class=3D"p">}</span>
  <span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">vir=
tual</span> <span class=3D"kt" style=3D"color: rgb(176, 0, 64);">void</span=
> <span class=3D"n">CollideWith</span><span class=3D"p">(</span><span class=
=3D"n">ApolloSpacecraft</span><span class=3D"o" style=3D"color: rgb(102, 10=
2, 102);">&amp;</span><span class=3D"p">)</span> <span class=3D"p">{</span>
    <span class=3D"n">cout</span> <span class=3D"o" style=3D"color: rgb(102=
, 102, 102);">&lt;&lt;</span> <span class=3D"s" style=3D"color: rgb(186, 33=
, 33);">"Asteroid hit an ApolloSpacecraft"</span> <span class=3D"o" style=
=3D"color: rgb(102, 102, 102);">&lt;&lt;</span> <span class=3D"n">endl</spa=
n><span class=3D"p">;</span>
  <span class=3D"p">}</span>
<span class=3D"p">};</span>

<span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">class=
</span> <span class=3D"nc" style=3D"color: rgb(0, 0, 255); font-weight: bol=
d;">ExplodingAsteroid</span> <span class=3D"o" style=3D"color: rgb(102, 102=
, 102);">:</span> <span class=3D"k" style=3D"color: rgb(0, 128, 0); font-we=
ight: bold;">public</span> <span class=3D"n">Asteroid</span> <span class=3D=
"p">{</span>
<span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">publi=
c</span><span class=3D"o" style=3D"color: rgb(102, 102, 102);">:</span>
  <span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">vir=
tual</span> <span class=3D"kt" style=3D"color: rgb(176, 0, 64);">void</span=
> <span class=3D"n">CollideWith</span><span class=3D"p">(</span><span class=
=3D"n">SpaceShip</span><span class=3D"o" style=3D"color: rgb(102, 102, 102)=
;">&amp;</span><span class=3D"p">)</span> <span class=3D"p">{</span>
    <span class=3D"n">cout</span> <span class=3D"o" style=3D"color: rgb(102=
, 102, 102);">&lt;&lt;</span> <span class=3D"s" style=3D"color: rgb(186, 33=
, 33);">"ExplodingAsteroid hit a SpaceShip"</span> <span class=3D"o" style=
=3D"color: rgb(102, 102, 102);">&lt;&lt;</span> <span class=3D"n">endl</spa=
n><span class=3D"p">;</span>
  <span class=3D"p">}</span>
  <span class=3D"k" style=3D"color: rgb(0, 128, 0); font-weight: bold;">vir=
tual</span> <span class=3D"kt" style=3D"color: rgb(176, 0, 64);">void</span=
> <span class=3D"n">CollideWith</span><span class=3D"p">(</span><span class=
=3D"n">ApolloSpacecraft</span><span class=3D"o" style=3D"color: rgb(102, 10=
2, 102);">&amp;</span><span class=3D"p">)</span> <span class=3D"p">{</span>
    <span class=3D"n">cout</span> <span class=3D"o" style=3D"color: rgb(102=
, 102, 102);">&lt;&lt;</span> <span class=3D"s" style=3D"color: rgb(186, 33=
, 33);">"ExplodingAsteroid hit an ApolloSpacecraft"</span> <span class=3D"o=
" style=3D"color: rgb(102, 102, 102);">&lt;&lt;</span> <span class=3D"n">en=
dl</span><span class=3D"p">;</span>
  <span class=3D"p">}</span>
<span class=3D"p">};</span></pre></div><div><br></div><div><pre style=3D"fo=
nt-family: monospace, Courier; color: rgb(0, 0, 0); background-color: rgb(2=
49, 249, 249); border-width: 1px; border-style: solid; border-color: rgb(22=
1, 221, 221); padding: 1em; white-space: pre-wrap; line-height: 1.3em; tab-=
size: 4; font-size: 14px;"><span class=3D"n">SpaceShip</span><span class=3D=
"o" style=3D"color: rgb(102, 102, 102);">&amp;</span> <span class=3D"n">the=
SpaceShipReference</span> <span class=3D"o" style=3D"color: rgb(102, 102, 1=
02);">=3D</span> <span class=3D"n">theApolloSpacecraft</span><span class=3D=
"p">;</span>
<span class=3D"n">theAsteroid</span><span class=3D"p">.</span><span class=
=3D"n">CollideWith</span><span class=3D"p">(</span><span class=3D"n">theSpa=
ceShipReference</span><span class=3D"p">);</span>
<span class=3D"n">theAsteroidReference</span><span class=3D"p">.</span><spa=
n class=3D"n">CollideWith</span><span class=3D"p">(</span><span class=3D"n"=
>theSpaceShipReference</span><span class=3D"p">);</span></pre><div>&nbsp;</=
div><div>This will not call the correct function even though we have a over=
load for it. Currently we need to employ a visitor / double dispatch to all=
 classes. In C# it is part of the language.&nbsp;</div><div>The visitor pat=
tern is often used and it would not be neccessary to implement it if we wou=
ld have a runtime cast. Read more on the motivation section for visitor on&=
nbsp;<a href=3D"https://en.wikipedia.org/wiki/Visitor_pattern#Motivation">w=
ikipedia</a></div><div>&nbsp;</div><blockquote class=3D"gmail_quote" style=
=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: =
1ex;">Em domingo, 16 de outubro de 2016, =C3=A0s 09:10:59 PDT, 'David Feurl=
e' via ISO C++=20
<br>Standard - Future Proposals escreveu:
<br>&gt; Hello,
<br>&gt;=20
<br>&gt; I like the works of C# dynamic which casts a variable to it's real=
 runtime
<br>&gt; type and I would like to see a similar feature in C++.
<br>
<br>You're describing something that C++ has already had since the very fir=
st=20
<br>version: virtual functions.
<br>
<br>&gt; Example in C#:
<br>&gt;=20
<br>&gt; Object o =3D new String("hahaha");
<br>&gt; dynamic d =3D o; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp=
;// the type of d is now
<br>&gt; string
<br>&gt; d +=3D "haha"; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp;//append something to
<br>&gt; the string
<br>&gt;=20
<br>&gt; As you can see the dynamic variable will be of the runtime type of=
 it's
<br>&gt; content. The following code uses the operators of string.
<br>
<br>In C++, we already have that without the need for a special keyword.
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object *o =3D new Deriv=
edObject("hahaha");
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*o +=3D "haha";
<br>
<br>Works just fine, provided that operator+=3D is a virtual function in Ob=
ject.
<br>
<br>&gt; The code that uses the dynamic variable could be compiled &nbsp;si=
nce all
<br>&gt; candidates are known at compiletime.
<br>
<br>Say what?=20
<br>
<br>&gt; Rtti could be used to determine the type at runtime.
<br>
<br>It already can be. In the example I gave above:
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object *o =3D new Deriv=
edObject("hahaha");
<br>
<br>We can assert:
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;typeid(*o) =3D=3D =
&amp;typeid(DerivedObject)
<br>
<br>&gt; Example in C++:
<br>&gt;=20
<br>&gt; struct Base {
<br>&gt; &nbsp; &nbsp; virtual Base() =3D default;
<br>
<br>You meant ~Base. You also need:
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;virtual void fun() =3D =
0;
<br>
<br>&gt; };
<br>&gt;=20
<br>&gt; struct A : public Base { void fun(); };
<br>&gt; struct B : public Base { void fun(); };
<br>&gt;=20
<br>&gt; fun(A*);
<br>&gt; fun(B*);
<br>&gt;=20
<br>&gt; Base* b =3D new A();
<br>&gt; auto dyn =3D runtime_cast&lt;A*, B*&gt;(b); &nbsp; // the type of =
dyn is either A* or B*
<br>&gt; if (!dyn) { return; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //the t=
ype of b was
<br>&gt; neither A* or B*
<br>&gt; dyn-&gt;fun(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; // either A::fun is
<br>&gt; called or B::fun
<br>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nb=
sp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &=
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=
//all possible
<br>
<br>Why do we need this? With the pure virtual like I added, you can write:
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Base *b =3D new A;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dyn-&gt;fun();
<br>
<br>&gt; What do you think about this?
<br>
<br>I think it's unnecessary to change what we already have.
<br>
<br>--=20
<br>Thiago Macieira - thiago (AT) <a href=3D"http://macieira.info" target=
=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D'http://www.google.=
com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dA=
FQjCNEswDUBNCNanbu7euhqLn_62FW8ag';return true;" onclick=3D"this.href=3D'ht=
tp://www.google.com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x3dD\x26sntz\=
x3d1\x26usg\x3dAFQjCNEswDUBNCNanbu7euhqLn_62FW8ag';return true;">macieira.i=
nfo</a> - thiago (AT) <a href=3D"http://kde.org" target=3D"_blank" rel=3D"n=
ofollow" onmousedown=3D"this.href=3D'http://www.google.com/url?q\x3dhttp%3A=
%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHGRJdo5_JYG1DowztwAHA=
Ks80XSA';return true;" onclick=3D"this.href=3D'http://www.google.com/url?q\=
x3dhttp%3A%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHGRJdo5_JYG=
1DowztwAHAKs80XSA';return true;">kde.org</a>
<br>&nbsp; &nbsp;Software Architect - Intel Open Source Technology Center
<br>
<br></blockquote><br>Am Sonntag, 16. Oktober 2016 19:22:31 UTC+2 schrieb Th=
iago Macieira:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Em domingo, 16 d=
e outubro de 2016, =C3=A0s 09:10:59 PDT, 'David Feurle' via ISO C++=20
<br>Standard - Future Proposals escreveu:
<br>&gt; Hello,
<br>&gt;=20
<br>&gt; I like the works of C# dynamic which casts a variable to it's real=
 runtime
<br>&gt; type and I would like to see a similar feature in C++.
<br>
<br>You're describing something that C++ has already had since the very fir=
st=20
<br>version: virtual functions.
<br>
<br>&gt; Example in C#:
<br>&gt;=20
<br>&gt; Object o =3D new String("hahaha");
<br>&gt; dynamic d =3D o; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp=
;// the type of d is now
<br>&gt; string
<br>&gt; d +=3D "haha"; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp;//append something to
<br>&gt; the string
<br>&gt;=20
<br>&gt; As you can see the dynamic variable will be of the runtime type of=
 it's
<br>&gt; content. The following code uses the operators of string.
<br>
<br>In C++, we already have that without the need for a special keyword.
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object *o =3D new Deriv=
edObject("hahaha");
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*o +=3D "haha";
<br>
<br>Works just fine, provided that operator+=3D is a virtual function in Ob=
ject.
<br>
<br>&gt; The code that uses the dynamic variable could be compiled &nbsp;si=
nce all
<br>&gt; candidates are known at compiletime.
<br>
<br>Say what?=20
<br>
<br>&gt; Rtti could be used to determine the type at runtime.
<br>
<br>It already can be. In the example I gave above:
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object *o =3D new Deriv=
edObject("hahaha");
<br>
<br>We can assert:
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;typeid(*o) =3D=3D =
&amp;typeid(DerivedObject)
<br>
<br>&gt; Example in C++:
<br>&gt;=20
<br>&gt; struct Base {
<br>&gt; &nbsp; &nbsp; virtual Base() =3D default;
<br>
<br>You meant ~Base. You also need:
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;virtual void fun() =3D =
0;
<br>
<br>&gt; };
<br>&gt;=20
<br>&gt; struct A : public Base { void fun(); };
<br>&gt; struct B : public Base { void fun(); };
<br>&gt;=20
<br>&gt; fun(A*);
<br>&gt; fun(B*);
<br>&gt;=20
<br>&gt; Base* b =3D new A();
<br>&gt; auto dyn =3D runtime_cast&lt;A*, B*&gt;(b); &nbsp; // the type of =
dyn is either A* or B*
<br>&gt; if (!dyn) { return; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //the t=
ype of b was
<br>&gt; neither A* or B*
<br>&gt; dyn-&gt;fun(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; // either A::fun is
<br>&gt; called or B::fun
<br>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nb=
sp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &=
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=
//all possible
<br>
<br>Why do we need this? With the pure virtual like I added, you can write:
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Base *b =3D new A;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dyn-&gt;fun();
<br>
<br>&gt; What do you think about this?
<br>
<br>I think it's unnecessary to change what we already have.
<br>
<br>--=20
<br>Thiago Macieira - thiago (AT) <a href=3D"http://macieira.info" target=
=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D'http://www.google.=
com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dA=
FQjCNEswDUBNCNanbu7euhqLn_62FW8ag';return true;" onclick=3D"this.href=3D'ht=
tp://www.google.com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x3dD\x26sntz\=
x3d1\x26usg\x3dAFQjCNEswDUBNCNanbu7euhqLn_62FW8ag';return true;">macieira.i=
nfo</a> - thiago (AT) <a href=3D"http://kde.org" target=3D"_blank" rel=3D"n=
ofollow" onmousedown=3D"this.href=3D'http://www.google.com/url?q\x3dhttp%3A=
%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHGRJdo5_JYG1DowztwAHA=
Ks80XSA';return true;" onclick=3D"this.href=3D'http://www.google.com/url?q\=
x3dhttp%3A%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHGRJdo5_JYG=
1DowztwAHAKs80XSA';return true;">kde.org</a>
<br>&nbsp; &nbsp;Software Architect - Intel Open Source Technology Center
<br>
<br></blockquote><br>Am Sonntag, 16. Oktober 2016 19:22:31 UTC+2 schrieb Th=
iago Macieira:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Em domingo, 16 d=
e outubro de 2016, =C3=A0s 09:10:59 PDT, 'David Feurle' via ISO C++=20
<br>Standard - Future Proposals escreveu:
<br>&gt; Hello,
<br>&gt;=20
<br>&gt; I like the works of C# dynamic which casts a variable to it's real=
 runtime
<br>&gt; type and I would like to see a similar feature in C++.
<br>
<br>You're describing something that C++ has already had since the very fir=
st=20
<br>version: virtual functions.
<br>
<br>&gt; Example in C#:
<br>&gt;=20
<br>&gt; Object o =3D new String("hahaha");
<br>&gt; dynamic d =3D o; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp=
;// the type of d is now
<br>&gt; string
<br>&gt; d +=3D "haha"; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp;//append something to
<br>&gt; the string
<br>&gt;=20
<br>&gt; As you can see the dynamic variable will be of the runtime type of=
 it's
<br>&gt; content. The following code uses the operators of string.
<br>
<br>In C++, we already have that without the need for a special keyword.
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object *o =3D new Deriv=
edObject("hahaha");
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*o +=3D "haha";
<br>
<br>Works just fine, provided that operator+=3D is a virtual function in Ob=
ject.
<br>
<br>&gt; The code that uses the dynamic variable could be compiled &nbsp;si=
nce all
<br>&gt; candidates are known at compiletime.
<br>
<br>Say what?=20
<br>
<br>&gt; Rtti could be used to determine the type at runtime.
<br>
<br>It already can be. In the example I gave above:
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object *o =3D new Deriv=
edObject("hahaha");
<br>
<br>We can assert:
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;typeid(*o) =3D=3D =
&amp;typeid(DerivedObject)
<br>
<br>&gt; Example in C++:
<br>&gt;=20
<br>&gt; struct Base {
<br>&gt; &nbsp; &nbsp; virtual Base() =3D default;
<br>
<br>You meant ~Base. You also need:
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;virtual void fun() =3D =
0;
<br>
<br>&gt; };
<br>&gt;=20
<br>&gt; struct A : public Base { void fun(); };
<br>&gt; struct B : public Base { void fun(); };
<br>&gt;=20
<br>&gt; fun(A*);
<br>&gt; fun(B*);
<br>&gt;=20
<br>&gt; Base* b =3D new A();
<br>&gt; auto dyn =3D runtime_cast&lt;A*, B*&gt;(b); &nbsp; // the type of =
dyn is either A* or B*
<br>&gt; if (!dyn) { return; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //the t=
ype of b was
<br>&gt; neither A* or B*
<br>&gt; dyn-&gt;fun(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; // either A::fun is
<br>&gt; called or B::fun
<br>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nb=
sp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &=
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=
//all possible
<br>
<br>Why do we need this? With the pure virtual like I added, you can write:
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Base *b =3D new A;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dyn-&gt;fun();
<br>
<br>&gt; What do you think about this?
<br>
<br>I think it's unnecessary to change what we already have.
<br>
<br>--=20
<br>Thiago Macieira - thiago (AT) <a href=3D"http://macieira.info" target=
=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D'http://www.google.=
com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dA=
FQjCNEswDUBNCNanbu7euhqLn_62FW8ag';return true;" onclick=3D"this.href=3D'ht=
tp://www.google.com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x3dD\x26sntz\=
x3d1\x26usg\x3dAFQjCNEswDUBNCNanbu7euhqLn_62FW8ag';return true;">macieira.i=
nfo</a> - thiago (AT) <a href=3D"http://kde.org" target=3D"_blank" rel=3D"n=
ofollow" onmousedown=3D"this.href=3D'http://www.google.com/url?q\x3dhttp%3A=
%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHGRJdo5_JYG1DowztwAHA=
Ks80XSA';return true;" onclick=3D"this.href=3D'http://www.google.com/url?q\=
x3dhttp%3A%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHGRJdo5_JYG=
1DowztwAHAKs80XSA';return true;">kde.org</a>
<br>&nbsp; &nbsp;Software Architect - Intel Open Source Technology Center
<br>
<br></blockquote><br>Am Sonntag, 16. Oktober 2016 19:22:31 UTC+2 schrieb Th=
iago Macieira:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Em domingo, 16 d=
e outubro de 2016, =C3=A0s 09:10:59 PDT, 'David Feurle' via ISO C++=20
<br>Standard - Future Proposals escreveu:
<br>&gt; Hello,
<br>&gt;=20
<br>&gt; I like the works of C# dynamic which casts a variable to it's real=
 runtime
<br>&gt; type and I would like to see a similar feature in C++.
<br>
<br>You're describing something that C++ has already had since the very fir=
st=20
<br>version: virtual functions.
<br>
<br>&gt; Example in C#:
<br>&gt;=20
<br>&gt; Object o =3D new String("hahaha");
<br>&gt; dynamic d =3D o; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp=
;// the type of d is now
<br>&gt; string
<br>&gt; d +=3D "haha"; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp;//append something to
<br>&gt; the string
<br>&gt;=20
<br>&gt; As you can see the dynamic variable will be of the runtime type of=
 it's
<br>&gt; content. The following code uses the operators of string.
<br>
<br>In C++, we already have that without the need for a special keyword.
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object *o =3D new Deriv=
edObject("hahaha");
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*o +=3D "haha";
<br>
<br>Works just fine, provided that operator+=3D is a virtual function in Ob=
ject.
<br>
<br>&gt; The code that uses the dynamic variable could be compiled &nbsp;si=
nce all
<br>&gt; candidates are known at compiletime.
<br>
<br>Say what?=20
<br>
<br>&gt; Rtti could be used to determine the type at runtime.
<br>
<br>It already can be. In the example I gave above:
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object *o =3D new Deriv=
edObject("hahaha");
<br>
<br>We can assert:
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;typeid(*o) =3D=3D =
&amp;typeid(DerivedObject)
<br>
<br>&gt; Example in C++:
<br>&gt;=20
<br>&gt; struct Base {
<br>&gt; &nbsp; &nbsp; virtual Base() =3D default;
<br>
<br>You meant ~Base. You also need:
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;virtual void fun() =3D =
0;
<br>
<br>&gt; };
<br>&gt;=20
<br>&gt; struct A : public Base { void fun(); };
<br>&gt; struct B : public Base { void fun(); };
<br>&gt;=20
<br>&gt; fun(A*);
<br>&gt; fun(B*);
<br>&gt;=20
<br>&gt; Base* b =3D new A();
<br>&gt; auto dyn =3D runtime_cast&lt;A*, B*&gt;(b); &nbsp; // the type of =
dyn is either A* or B*
<br>&gt; if (!dyn) { return; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //the t=
ype of b was
<br>&gt; neither A* or B*
<br>&gt; dyn-&gt;fun(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; // either A::fun is
<br>&gt; called or B::fun
<br>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nb=
sp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &=
nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=
//all possible
<br>
<br>Why do we need this? With the pure virtual like I added, you can write:
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Base *b =3D new A;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dyn-&gt;fun();
<br>
<br>&gt; What do you think about this?
<br>
<br>I think it's unnecessary to change what we already have.
<br>
<br>--=20
<br>Thiago Macieira - thiago (AT) <a href=3D"http://macieira.info" target=
=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D'http://www.google.=
com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dA=
FQjCNEswDUBNCNanbu7euhqLn_62FW8ag';return true;" onclick=3D"this.href=3D'ht=
tp://www.google.com/url?q\x3dhttp%3A%2F%2Fmacieira.info\x26sa\x3dD\x26sntz\=
x3d1\x26usg\x3dAFQjCNEswDUBNCNanbu7euhqLn_62FW8ag';return true;">macieira.i=
nfo</a> - thiago (AT) <a href=3D"http://kde.org" target=3D"_blank" rel=3D"n=
ofollow" onmousedown=3D"this.href=3D'http://www.google.com/url?q\x3dhttp%3A=
%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHGRJdo5_JYG1DowztwAHA=
Ks80XSA';return true;" onclick=3D"this.href=3D'http://www.google.com/url?q\=
x3dhttp%3A%2F%2Fkde.org\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHGRJdo5_JYG=
1DowztwAHAKs80XSA';return true;">kde.org</a>
<br>&nbsp; &nbsp;Software Architect - Intel Open Source Technology Center
<br>
<br></blockquote></div></div>

<p></p>

-- <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 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/39dc6003-567c-4506-9fbb-2e0340313960%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter">https://groups.goo=
gle.com/a/isocpp.org/d/msgid/std-proposals/39dc6003-567c-4506-9fbb-2e034031=
3960%40isocpp.org</a>.<br>
<br><!--end of _originalContent --></div></body></html>

<p></p>

-- <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+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/20161016175706.4907089.62737.18585%40=
gmail.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com=
/a/isocpp.org/d/msgid/std-proposals/20161016175706.4907089.62737.18585%40gm=
ail.com</a>.<br />

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Sun, 16 Oct 2016 11:03:13 -0700 (PDT)
Raw View
------=_Part_426_1526941359.1476640994077
Content-Type: multipart/alternative;
 boundary="----=_Part_427_421559094.1476640994078"

------=_Part_427_421559094.1476640994078
Content-Type: text/plain; charset=UTF-8



On Sunday, October 16, 2016 at 1:44:01 PM UTC-4, David Feurle wrote:
>
> Hello Thiago,
>
> there are things that can not be done with virtual functions. For example double
> dispatch
> <https://en.wikipedia.org/wiki/Double_dispatch#Double_dispatch_is_more_than_function_overloading> and
> the visitor
> <https://en.wikipedia.org/wiki/Visitor_pattern#C.2B.2B_example>.
>
> It is possible to simulate a dynamic call functionality with virtual
> functions. But it is a hazzle and several classes / functions need to be
> implemented to add this functionality.
>
> Imagine the following "standard" example for double dispatch:
>
> class SpaceShip {};class ApolloSpacecraft : public SpaceShip {};
> class Asteroid {public:
>   virtual void CollideWith(SpaceShip&) {
>     cout << "Asteroid hit a SpaceShip" << endl;
>   }
>   virtual void CollideWith(ApolloSpacecraft&) {
>     cout << "Asteroid hit an ApolloSpacecraft" << endl;
>   }};
> class ExplodingAsteroid : public Asteroid {public:
>   virtual void CollideWith(SpaceShip&) {
>     cout << "ExplodingAsteroid hit a SpaceShip" << endl;
>   }
>   virtual void CollideWith(ApolloSpacecraft&) {
>     cout << "ExplodingAsteroid hit an ApolloSpacecraft" << endl;
>   }};
>
>
> SpaceShip& theSpaceShipReference = theApolloSpacecraft;theAsteroid.CollideWith(theSpaceShipReference);theAsteroidReference.CollideWith(theSpaceShipReference);
>
>
> This will not call the correct function even though we have a overload for
> it. Currently we need to employ a visitor / double dispatch to all classes.
>

Or you could just not poorly design your collision system.


> In C# it is part of the language.
> The visitor pattern is often used and it would not be neccessary to
> implement it if we would have a runtime cast.
>

Alternatively, we could just make visitation more reasonable to implement.

C++ is a statically typed language. What you're talking about requires
dynamic dispatching of a kind that C++ as a language is simply not capable
of. Not without adding a bunch of hacks to the language.

Take your specific case, for example: `dyn->fun()`, where `dyn` may be `A*`
or `B*`. Because C++ is a statically typed language, it cannot have a type
which may be one of two things. Therefore, it must be a type which is
neither, like a `variant` or somesuch. This type will therefore have to
*synthesize* replicas of all of the operations that could be performed on
any of those types. Even "operator-dot" gymnastics don't permit that.

Plus, `A::fun` and `B::fun` are allowed to return different things. Even if
they're both virtual functions inherited from the same base class, the
derived class versions can differ in terms of return values. So what does
`dyn->fun()` return? It would have to return a `variant` of the possible
return values. Which makes it hard to use those return values.

This is far more complex than you give it credit for being. C# is a much
more malleable language that C++.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/09d1c64d-9207-4b2b-b134-45cb5cbbdf9e%40isocpp.org.

------=_Part_427_421559094.1476640994078
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Sunday, October 16, 2016 at 1:44:01 PM UTC-4, D=
avid Feurle wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;marg=
in-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"=
ltr">Hello Thiago,=C2=A0<div><br></div><div>there are things that can not b=
e done with virtual functions. For example=C2=A0<a href=3D"https://en.wikip=
edia.org/wiki/Double_dispatch#Double_dispatch_is_more_than_function_overloa=
ding" target=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;ht=
tps://www.google.com/url?q\x3dhttps%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDoubl=
e_dispatch%23Double_dispatch_is_more_than_function_overloading\x26sa\x3dD\x=
26sntz\x3d1\x26usg\x3dAFQjCNHn84xueQUUGYt8kIEgO-VrxiRL_g&#39;;return true;"=
 onclick=3D"this.href=3D&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2F=
en.wikipedia.org%2Fwiki%2FDouble_dispatch%23Double_dispatch_is_more_than_fu=
nction_overloading\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHn84xueQUUGYt8kI=
EgO-VrxiRL_g&#39;;return true;">double dispatch</a>=C2=A0and the=C2=A0<a hr=
ef=3D"https://en.wikipedia.org/wiki/Visitor_pattern#C.2B.2B_example" target=
=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;https://www.go=
ogle.com/url?q\x3dhttps%3A%2F%2Fen.wikipedia.org%2Fwiki%2FVisitor_pattern%2=
3C.2B.2B_example\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHDogdkey6nkQiRpI_O=
ONCREBEULw&#39;;return true;" onclick=3D"this.href=3D&#39;https://www.googl=
e.com/url?q\x3dhttps%3A%2F%2Fen.wikipedia.org%2Fwiki%2FVisitor_pattern%23C.=
2B.2B_example\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHDogdkey6nkQiRpI_OONC=
REBEULw&#39;;return true;">visitor</a>.</div><div><br></div><div>It is poss=
ible to simulate a dynamic call functionality with virtual functions. But i=
t is a hazzle and several classes / functions need to be implemented to add=
 this functionality.</div><div><br></div><div>Imagine the following &quot;s=
tandard&quot; example for double dispatch:</div><div><br></div><div><pre st=
yle=3D"font-family:monospace,Courier;color:rgb(0,0,0);background-color:rgb(=
249,249,249);border-width:1px;border-style:solid;border-color:rgb(221,221,2=
21);padding:1em;white-space:pre-wrap;line-height:1.3em;font-size:14px"><spa=
n style=3D"color:rgb(0,128,0);font-weight:bold">class</span> <span style=3D=
"color:rgb(0,0,255);font-weight:bold">SpaceShip</span> <span>{};</span>
<span style=3D"color:rgb(0,128,0);font-weight:bold">class</span> <span styl=
e=3D"color:rgb(0,0,255);font-weight:bold">ApolloSpacecraft</span> <span sty=
le=3D"color:rgb(102,102,102)">:</span> <span style=3D"color:rgb(0,128,0);fo=
nt-weight:bold">public</span> <span>SpaceShip</span> <span>{};</span>

<span style=3D"color:rgb(0,128,0);font-weight:bold">class</span> <span styl=
e=3D"color:rgb(0,0,255);font-weight:bold">Asteroid</span> <span>{</span>
<span style=3D"color:rgb(0,128,0);font-weight:bold">public</span><span styl=
e=3D"color:rgb(102,102,102)">:</span>
  <span style=3D"color:rgb(0,128,0);font-weight:bold">virtual</span> <span =
style=3D"color:rgb(176,0,64)">void</span> <span>CollideWith</span><span>(</=
span><span>SpaceShip</span><span style=3D"color:rgb(102,102,102)">&amp;</sp=
an><span>)</span> <span>{</span>
    <span>cout</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</span=
> <span style=3D"color:rgb(186,33,33)">&quot;Asteroid hit a SpaceShip&quot;=
</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</span> <span>endl</=
span><span>;</span>
  <span>}</span>
  <span style=3D"color:rgb(0,128,0);font-weight:bold">virtual</span> <span =
style=3D"color:rgb(176,0,64)">void</span> <span>CollideWith</span><span>(</=
span><span>ApolloSpacecraft</span><span style=3D"color:rgb(102,102,102)">&a=
mp;</span><span>)</span> <span>{</span>
    <span>cout</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</span=
> <span style=3D"color:rgb(186,33,33)">&quot;Asteroid hit an ApolloSpacecra=
ft&quot;</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</span> <spa=
n>endl</span><span>;</span>
  <span>}</span>
<span>};</span>

<span style=3D"color:rgb(0,128,0);font-weight:bold">class</span> <span styl=
e=3D"color:rgb(0,0,255);font-weight:bold">ExplodingAsteroid</span> <span st=
yle=3D"color:rgb(102,102,102)">:</span> <span style=3D"color:rgb(0,128,0);f=
ont-weight:bold">public</span> <span>Asteroid</span> <span>{</span>
<span style=3D"color:rgb(0,128,0);font-weight:bold">public</span><span styl=
e=3D"color:rgb(102,102,102)">:</span>
  <span style=3D"color:rgb(0,128,0);font-weight:bold">virtual</span> <span =
style=3D"color:rgb(176,0,64)">void</span> <span>CollideWith</span><span>(</=
span><span>SpaceShip</span><span style=3D"color:rgb(102,102,102)">&amp;</sp=
an><span>)</span> <span>{</span>
    <span>cout</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</span=
> <span style=3D"color:rgb(186,33,33)">&quot;ExplodingAsteroid hit a SpaceS=
hip&quot;</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</span> <sp=
an>endl</span><span>;</span>
  <span>}</span>
  <span style=3D"color:rgb(0,128,0);font-weight:bold">virtual</span> <span =
style=3D"color:rgb(176,0,64)">void</span> <span>CollideWith</span><span>(</=
span><span>ApolloSpacecraft</span><span style=3D"color:rgb(102,102,102)">&a=
mp;</span><span>)</span> <span>{</span>
    <span>cout</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</span=
> <span style=3D"color:rgb(186,33,33)">&quot;ExplodingAsteroid hit an Apoll=
oSpacecraft&quot;</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</s=
pan> <span>endl</span><span>;</span>
  <span>}</span>
<span>};</span></pre></div><div><br></div><div><pre style=3D"font-family:mo=
nospace,Courier;color:rgb(0,0,0);background-color:rgb(249,249,249);border-w=
idth:1px;border-style:solid;border-color:rgb(221,221,221);padding:1em;white=
-space:pre-wrap;line-height:1.3em;font-size:14px"><span>SpaceShip</span><sp=
an style=3D"color:rgb(102,102,102)">&amp;</span> <span>theSpaceShipReferenc=
e</span> <span style=3D"color:rgb(102,102,102)">=3D</span> <span>theApolloS=
pacecraft</span><span>;</span>
<span>theAsteroid</span><span>.</span><span>CollideWith</span><span>(</span=
><span>theSpa<wbr>ceShipReference</span><span>);</span>
<span>theAsteroidReference</span><span>.</span><span>CollideWi<wbr>th</span=
><span>(</span><span>theSpaceShipReference</span><span>);</span></pre><div>=
=C2=A0</div><div>This will not call the correct function even though we hav=
e a overload for it. Currently we need to employ a visitor / double dispatc=
h to all classes.</div></div></div></blockquote><div><br>Or you could just =
not poorly design your collision system.<br>=C2=A0</div><blockquote class=
=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #cc=
c solid;padding-left: 1ex;"><div dir=3D"ltr"><div><div>In C# it is part of =
the language.=C2=A0</div><div>The visitor pattern is often used and it woul=
d not be neccessary to implement it if we would have a runtime cast.</div><=
/div></div></blockquote><div><br>Alternatively, we could just make visitati=
on more reasonable to implement.<br><br>C++ is a statically typed language.=
 What you&#39;re talking about requires dynamic dispatching of a kind that =
C++ as a language is simply not capable of. Not without adding a bunch of h=
acks to the language.<br><br>Take your specific case, for example: `dyn-&gt=
;fun()`, where `dyn` may be `A*` or `B*`. Because C++ is a statically typed=
 language, it cannot have a type which may be one of two things. Therefore,=
 it must be a type which is neither, like a `variant` or somesuch. This typ=
e will therefore have to <i>synthesize</i> replicas of all of the operation=
s that could be performed on any of those types. Even &quot;operator-dot&qu=
ot; gymnastics don&#39;t permit that.<br><br>Plus, `A::fun` and `B::fun` ar=
e allowed to return different things. Even if they&#39;re both virtual func=
tions inherited from the same base class, the derived class versions can di=
ffer in terms of return values. So what does `dyn-&gt;fun()` return? It wou=
ld have to return a `variant` of the possible return values. Which makes it=
 hard to use those return values.<br><br>This is far more complex than you =
give it credit for being. C# is a much more malleable language that C++.<br=
></div></div>

<p></p>

-- <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+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/09d1c64d-9207-4b2b-b134-45cb5cbbdf9e%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/09d1c64d-9207-4b2b-b134-45cb5cbbdf9e=
%40isocpp.org</a>.<br />

------=_Part_427_421559094.1476640994078--

------=_Part_426_1526941359.1476640994077--

.


Author: "'David Feurle' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Sun, 16 Oct 2016 11:17:20 -0700 (PDT)
Raw View
------=_Part_1547_875629730.1476641840489
Content-Type: multipart/alternative;
 boundary="----=_Part_1548_1552740053.1476641840490"

------=_Part_1548_1552740053.1476641840490
Content-Type: text/plain; charset=UTF-8



Am Sonntag, 16. Oktober 2016 20:03:14 UTC+2 schrieb Nicol Bolas:
>
>
>
> On Sunday, October 16, 2016 at 1:44:01 PM UTC-4, David Feurle wrote:
>>
>> Hello Thiago,
>>
>> there are things that can not be done with virtual functions. For example double
>> dispatch
>> <https://en.wikipedia.org/wiki/Double_dispatch#Double_dispatch_is_more_than_function_overloading> and
>> the visitor
>> <https://en.wikipedia.org/wiki/Visitor_pattern#C.2B.2B_example>.
>>
>> It is possible to simulate a dynamic call functionality with virtual
>> functions. But it is a hazzle and several classes / functions need to be
>> implemented to add this functionality.
>>
>> Imagine the following "standard" example for double dispatch:
>>
>> class SpaceShip {};class ApolloSpacecraft : public SpaceShip {};
>> class Asteroid {public:
>>   virtual void CollideWith(SpaceShip&) {
>>     cout << "Asteroid hit a SpaceShip" << endl;
>>   }
>>   virtual void CollideWith(ApolloSpacecraft&) {
>>     cout << "Asteroid hit an ApolloSpacecraft" << endl;
>>   }};
>> class ExplodingAsteroid : public Asteroid {public:
>>   virtual void CollideWith(SpaceShip&) {
>>     cout << "ExplodingAsteroid hit a SpaceShip" << endl;
>>   }
>>   virtual void CollideWith(ApolloSpacecraft&) {
>>     cout << "ExplodingAsteroid hit an ApolloSpacecraft" << endl;
>>   }};
>>
>>
>> SpaceShip& theSpaceShipReference = theApolloSpacecraft;theAsteroid.CollideWith(theSpaceShipReference);theAsteroidReference.CollideWith(theSpaceShipReference);
>>
>>
>> This will not call the correct function even though we have a overload
>> for it. Currently we need to employ a visitor / double dispatch to all
>> classes.
>>
>
> Or you could just not poorly design your collision system.
>

It is just a standard example but not the one that makes the most sense -
I'll give you that. There are situations where visitor makes a lot of
sense. Imagine you recieve messages from the network. Different network
handlers need to react to the messages received differently. It is not
possible to add the behaviour to the message (lots of different handlers)
and the handler needs to dispatch all the messages according to it's
(unknown) type.

>
>
>> In C# it is part of the language.
>> The visitor pattern is often used and it would not be neccessary to
>> implement it if we would have a runtime cast.
>>
>
> Alternatively, we could just make visitation more reasonable to implement.
>
> How could that be?


> C++ is a statically typed language. What you're talking about requires
> dynamic dispatching of a kind that C++ as a language is simply not capable
> of. Not without adding a bunch of hacks to the language.
>
> Take your specific case, for example: `dyn->fun()`, where `dyn` may be
> `A*` or `B*`. Because C++ is a statically typed language, it cannot have a
> type which may be one of two things. Therefore, it must be a type which is
> neither, like a `variant` or somesuch. This type will therefore have to
> *synthesize* replicas of all of the operations that could be performed on
> any of those types. Even "operator-dot" gymnastics don't permit that.
>
> Plus, `A::fun` and `B::fun` are allowed to return different things. Even
> if they're both virtual functions inherited from the same base class, the
> derived class versions can differ in terms of return values. So what does
> `dyn->fun()` return? It would have to return a `variant` of the possible
> return values. Which makes it hard to use those return values.
>
> This is far more complex than you give it credit for being. C# is a much
> more malleable language that C++.
>

What about making the scope that the runtime_cast is compiled with all the
possible types. The candidates are all there. The variations in the return
value does not make any problem than. One could think about it similar to a
generic lambda - beeing compiled statically for all types it is used with.


--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/81d1f92d-34b7-47b8-822c-a147e3a7ef32%40isocpp.org.

------=_Part_1548_1552740053.1476641840490
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>Am Sonntag, 16. Oktober 2016 20:03:14 UTC+2 schrie=
b Nicol Bolas:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"=
><br><br>On Sunday, October 16, 2016 at 1:44:01 PM UTC-4, David Feurle wrot=
e:<blockquote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;bor=
der-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">Hello Thiago,=C2=
=A0<div><br></div><div>there are things that can not be done with virtual f=
unctions. For example=C2=A0<a href=3D"https://en.wikipedia.org/wiki/Double_=
dispatch#Double_dispatch_is_more_than_function_overloading" rel=3D"nofollow=
" target=3D"_blank" onmousedown=3D"this.href=3D&#39;https://www.google.com/=
url?q\x3dhttps%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDouble_dispatch%23Double_d=
ispatch_is_more_than_function_overloading\x26sa\x3dD\x26sntz\x3d1\x26usg\x3=
dAFQjCNHn84xueQUUGYt8kIEgO-VrxiRL_g&#39;;return true;" onclick=3D"this.href=
=3D&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fen.wikipedia.org%2Fwi=
ki%2FDouble_dispatch%23Double_dispatch_is_more_than_function_overloading\x2=
6sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHn84xueQUUGYt8kIEgO-VrxiRL_g&#39;;ret=
urn true;">double dispatch</a>=C2=A0and the=C2=A0<a href=3D"https://en.wiki=
pedia.org/wiki/Visitor_pattern#C.2B.2B_example" rel=3D"nofollow" target=3D"=
_blank" onmousedown=3D"this.href=3D&#39;https://www.google.com/url?q\x3dhtt=
ps%3A%2F%2Fen.wikipedia.org%2Fwiki%2FVisitor_pattern%23C.2B.2B_example\x26s=
a\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHDogdkey6nkQiRpI_OONCREBEULw&#39;;retur=
n true;" onclick=3D"this.href=3D&#39;https://www.google.com/url?q\x3dhttps%=
3A%2F%2Fen.wikipedia.org%2Fwiki%2FVisitor_pattern%23C.2B.2B_example\x26sa\x=
3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHDogdkey6nkQiRpI_OONCREBEULw&#39;;return t=
rue;">visitor</a>.</div><div><br></div><div>It is possible to simulate a dy=
namic call functionality with virtual functions. But it is a hazzle and sev=
eral classes / functions need to be implemented to add this functionality.<=
/div><div><br></div><div>Imagine the following &quot;standard&quot; example=
 for double dispatch:</div><div><br></div><div><pre style=3D"font-family:mo=
nospace,Courier;color:rgb(0,0,0);background-color:rgb(249,249,249);border-w=
idth:1px;border-style:solid;border-color:rgb(221,221,221);padding:1em;white=
-space:pre-wrap;line-height:1.3em;font-size:14px"><span style=3D"color:rgb(=
0,128,0);font-weight:bold">class</span> <span style=3D"color:rgb(0,0,255);f=
ont-weight:bold">SpaceShip</span> <span>{};</span>
<span style=3D"color:rgb(0,128,0);font-weight:bold">class</span> <span styl=
e=3D"color:rgb(0,0,255);font-weight:bold">ApolloSpacecraft</span> <span sty=
le=3D"color:rgb(102,102,102)">:</span> <span style=3D"color:rgb(0,128,0);fo=
nt-weight:bold">public</span> <span>SpaceShip</span> <span>{};</span>

<span style=3D"color:rgb(0,128,0);font-weight:bold">class</span> <span styl=
e=3D"color:rgb(0,0,255);font-weight:bold">Asteroid</span> <span>{</span>
<span style=3D"color:rgb(0,128,0);font-weight:bold">public</span><span styl=
e=3D"color:rgb(102,102,102)">:</span>
  <span style=3D"color:rgb(0,128,0);font-weight:bold">virtual</span> <span =
style=3D"color:rgb(176,0,64)">void</span> <span>CollideWith</span><span>(</=
span><span>SpaceShip</span><span style=3D"color:rgb(102,102,102)">&amp;</sp=
an><span>)</span> <span>{</span>
    <span>cout</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</span=
> <span style=3D"color:rgb(186,33,33)">&quot;Asteroid hit a SpaceShip&quot;=
</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</span> <span>endl</=
span><span>;</span>
  <span>}</span>
  <span style=3D"color:rgb(0,128,0);font-weight:bold">virtual</span> <span =
style=3D"color:rgb(176,0,64)">void</span> <span>CollideWith</span><span>(</=
span><span>ApolloSpacecraft</span><span style=3D"color:rgb(102,102,102)">&a=
mp;</span><span>)</span> <span>{</span>
    <span>cout</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</span=
> <span style=3D"color:rgb(186,33,33)">&quot;Asteroid hit an ApolloSpacecra=
ft&quot;</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</span> <spa=
n>endl</span><span>;</span>
  <span>}</span>
<span>};</span>

<span style=3D"color:rgb(0,128,0);font-weight:bold">class</span> <span styl=
e=3D"color:rgb(0,0,255);font-weight:bold">ExplodingAsteroid</span> <span st=
yle=3D"color:rgb(102,102,102)">:</span> <span style=3D"color:rgb(0,128,0);f=
ont-weight:bold">public</span> <span>Asteroid</span> <span>{</span>
<span style=3D"color:rgb(0,128,0);font-weight:bold">public</span><span styl=
e=3D"color:rgb(102,102,102)">:</span>
  <span style=3D"color:rgb(0,128,0);font-weight:bold">virtual</span> <span =
style=3D"color:rgb(176,0,64)">void</span> <span>CollideWith</span><span>(</=
span><span>SpaceShip</span><span style=3D"color:rgb(102,102,102)">&amp;</sp=
an><span>)</span> <span>{</span>
    <span>cout</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</span=
> <span style=3D"color:rgb(186,33,33)">&quot;ExplodingAsteroid hit a SpaceS=
hip&quot;</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</span> <sp=
an>endl</span><span>;</span>
  <span>}</span>
  <span style=3D"color:rgb(0,128,0);font-weight:bold">virtual</span> <span =
style=3D"color:rgb(176,0,64)">void</span> <span>CollideWith</span><span>(</=
span><span>ApolloSpacecraft</span><span style=3D"color:rgb(102,102,102)">&a=
mp;</span><span>)</span> <span>{</span>
    <span>cout</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</span=
> <span style=3D"color:rgb(186,33,33)">&quot;ExplodingAsteroid hit an Apoll=
oSpacecraft&quot;</span> <span style=3D"color:rgb(102,102,102)">&lt;&lt;</s=
pan> <span>endl</span><span>;</span>
  <span>}</span>
<span>};</span></pre></div><div><br></div><div><pre style=3D"font-family:mo=
nospace,Courier;color:rgb(0,0,0);background-color:rgb(249,249,249);border-w=
idth:1px;border-style:solid;border-color:rgb(221,221,221);padding:1em;white=
-space:pre-wrap;line-height:1.3em;font-size:14px"><span>SpaceShip</span><sp=
an style=3D"color:rgb(102,102,102)">&amp;</span> <span>theSpaceShipReferenc=
e</span> <span style=3D"color:rgb(102,102,102)">=3D</span> <span>theApolloS=
pacecraft</span><span>;</span>
<span>theAsteroid</span><span>.</span><span>CollideWith</span><span>(</span=
><span>theSpa<wbr>ceShipReference</span><span>);</span>
<span>theAsteroidReference</span><span>.</span><span>CollideWi<wbr>th</span=
><span>(</span><span>theSpaceShipReference</span><span>);</span></pre><div>=
=C2=A0</div><div>This will not call the correct function even though we hav=
e a overload for it. Currently we need to employ a visitor / double dispatc=
h to all classes.</div></div></div></blockquote><div><br>Or you could just =
not poorly design your collision system.<br></div></div></blockquote><div><=
br></div><div>It is just a standard example but not the one that makes the =
most sense - I&#39;ll give you that. There are situations where visitor mak=
es a lot of sense. Imagine you recieve messages from the network. Different=
 network handlers need to react to the messages received differently. It is=
 not possible to add the behaviour to the message (lots of different handle=
rs) and the handler needs to dispatch all the messages according to it&#39;=
s (unknown) type.</div><blockquote class=3D"gmail_quote" style=3D"margin: 0=
;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div di=
r=3D"ltr"><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margi=
n:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=
=3D"ltr"><div><div>In C# it is part of the language.=C2=A0</div><div>The vi=
sitor pattern is often used and it would not be neccessary to implement it =
if we would have a runtime cast.</div></div></div></blockquote><div><br>Alt=
ernatively, we could just make visitation more reasonable to implement.<br>=
<br></div></div></blockquote><div>How could that be?</div><div>=C2=A0</div>=
<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;bor=
der-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><div>C++ is a=
 statically typed language. What you&#39;re talking about requires dynamic =
dispatching of a kind that C++ as a language is simply not capable of. Not =
without adding a bunch of hacks to the language.<br><br>Take your specific =
case, for example: `dyn-&gt;fun()`, where `dyn` may be `A*` or `B*`. Becaus=
e C++ is a statically typed language, it cannot have a type which may be on=
e of two things. Therefore, it must be a type which is neither, like a `var=
iant` or somesuch. This type will therefore have to <i>synthesize</i> repli=
cas of all of the operations that could be performed on any of those types.=
 Even &quot;operator-dot&quot; gymnastics don&#39;t permit that.<br><br>Plu=
s, `A::fun` and `B::fun` are allowed to return different things. Even if th=
ey&#39;re both virtual functions inherited from the same base class, the de=
rived class versions can differ in terms of return values. So what does `dy=
n-&gt;fun()` return? It would have to return a `variant` of the possible re=
turn values. Which makes it hard to use those return values.<br><br>This is=
 far more complex than you give it credit for being. C# is a much more mall=
eable language that C++.<br></div></div></blockquote><div><br></div><div>Wh=
at about making the scope that the runtime_cast is compiled with all the po=
ssible types. The candidates are all there. The variations in the return va=
lue does not make any problem than. One could think about it similar to a g=
eneric lambda - beeing compiled statically for all types it is used with.</=
div><div>=C2=A0<br></div></div>

<p></p>

-- <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+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/81d1f92d-34b7-47b8-822c-a147e3a7ef32%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/81d1f92d-34b7-47b8-822c-a147e3a7ef32=
%40isocpp.org</a>.<br />

------=_Part_1548_1552740053.1476641840490--

------=_Part_1547_875629730.1476641840489--

.


Author: "D. B." <db0451@gmail.com>
Date: Sun, 16 Oct 2016 19:19:57 +0100
Raw View
--047d7bd91ac0bee9a6053eff8191
Content-Type: text/plain; charset=UTF-8

It sounds like you basically want a way to automatically implement the old
pattern of 'get typeinfo then do a huge switch statement'. This, while not
seeming unreasonable initially, usually indicates larger design issues,
which can typically be done better than with RTTI and a huge switch
statement anyway.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhFZg-YLL6vUP%3D7c7or4JtamTG7wFeF%2BP0VxEnoJLAVUPA%40mail.gmail.com.

--047d7bd91ac0bee9a6053eff8191
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">It sounds like you basically want a way to automatically i=
mplement the old pattern of &#39;get typeinfo then do a huge switch stateme=
nt&#39;. This, while not seeming unreasonable initially, usually indicates =
larger design issues, which can typically be done better than with RTTI and=
 a huge switch statement anyway.<br></div>

<p></p>

-- <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+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/CACGiwhFZg-YLL6vUP%3D7c7or4JtamTG7wFe=
F%2BP0VxEnoJLAVUPA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter"=
>https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhFZg-YL=
L6vUP%3D7c7or4JtamTG7wFeF%2BP0VxEnoJLAVUPA%40mail.gmail.com</a>.<br />

--047d7bd91ac0bee9a6053eff8191--

.


Author: "'David Feurle' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Sun, 16 Oct 2016 11:36:59 -0700 (PDT)
Raw View
------=_Part_1553_1616549436.1476643019817
Content-Type: multipart/alternative;
 boundary="----=_Part_1554_680024363.1476643019818"

------=_Part_1554_680024363.1476643019818
Content-Type: text/plain; charset=UTF-8

A switch statement on the type is always a bad design - agreed. But using a
visitor is not. In fact it is one of the most used / important patterns in
C++. The runtime cast saves the user of the C++ statement to implement the
visitor him/herselve and adds it as a language feature. Shorter and more
precise. This of course is only an Opinion

Am Sonntag, 16. Oktober 2016 20:20:01 UTC+2 schrieb D. B.:
>
> It sounds like you basically want a way to automatically implement the old
> pattern of 'get typeinfo then do a huge switch statement'. This, while not
> seeming unreasonable initially, usually indicates larger design issues,
> which can typically be done better than with RTTI and a huge switch
> statement anyway.
>

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1833a175-cc0a-4d24-9819-7f146d19a032%40isocpp.org.

------=_Part_1554_680024363.1476643019818
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">A switch statement on the type is always a bad design - ag=
reed. But using a visitor is not. In fact it is one of the most used / impo=
rtant patterns in C++. The runtime cast saves the user of the C++ statement=
 to implement the visitor him/herselve and adds it as a language feature. S=
horter and more precise. This of course is only an Opinion<br><br>Am Sonnta=
g, 16. Oktober 2016 20:20:01 UTC+2 schrieb D. B.:<blockquote class=3D"gmail=
_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;p=
adding-left: 1ex;"><div dir=3D"ltr">It sounds like you basically want a way=
 to automatically implement the old pattern of &#39;get typeinfo then do a =
huge switch statement&#39;. This, while not seeming unreasonable initially,=
 usually indicates larger design issues, which can typically be done better=
 than with RTTI and a huge switch statement anyway.<br></div>
</blockquote></div>

<p></p>

-- <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+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/1833a175-cc0a-4d24-9819-7f146d19a032%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/1833a175-cc0a-4d24-9819-7f146d19a032=
%40isocpp.org</a>.<br />

------=_Part_1554_680024363.1476643019818--

------=_Part_1553_1616549436.1476643019817--

.


Author: Giovanni Piero Deretta <gpderetta@gmail.com>
Date: Sun, 16 Oct 2016 14:04:22 -0700 (PDT)
Raw View
------=_Part_1674_596928945.1476651862767
Content-Type: multipart/alternative;
 boundary="----=_Part_1675_843545225.1476651862767"

------=_Part_1675_843545225.1476651862767
Content-Type: text/plain; charset=UTF-8

On Sunday, October 16, 2016 at 7:03:14 PM UTC+1, Nicol Bolas wrote:
[...]

> C++ is a statically typed language. What you're talking about requires
> dynamic dispatching of a kind that C++ as a language is simply not capable
> of. Not without adding a bunch of hacks to the language.
>
>
C++ has dynamic dispatching, but it is restricted only on the first
parameter of virtual function calls. There was a paper sometimes ago
Stroustrup about a possible efficient implementation of multiple dispatch
for C++ [1], but there isn't really enough support to get it into the
language.

C# dynamic is a great feature, but to perform decently needs a powerful
runtime/JIT. I do not think it would work well in C++. Multimethods would
be nice though.

[1]  Open Multimethods in C++.

-- gpd

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/de8050cf-117b-46e6-b2de-329db8d2bfca%40isocpp.org.

------=_Part_1675_843545225.1476651862767
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Sunday, October 16, 2016 at 7:03:14 PM UTC+1, Nicol Bol=
as wrote:<div>[...] <br></div><blockquote class=3D"gmail_quote" style=3D"ma=
rgin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">=
<div dir=3D"ltr">C++ is a statically typed language. What you&#39;re talkin=
g about requires dynamic dispatching of a kind that C++ as a language is si=
mply not capable of. Not without adding a bunch of hacks to the language.<b=
r><div><br></div></div></blockquote><div><br>C++ has dynamic dispatching, b=
ut it is restricted only on the first parameter of virtual function calls. =
There was a paper sometimes ago Stroustrup about a possible efficient imple=
mentation of multiple dispatch for C++ [1], but there isn&#39;t really enou=
gh support to get it into the language. <br><br>C# dynamic is a great featu=
re, but to perform decently needs a powerful runtime/JIT. I do not think it=
 would work well in C++. Multimethods would be nice though.<br><br>[1]=C2=
=A0 Open Multimethods in C++.<br><br>-- gpd<br></div></div>

<p></p>

-- <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+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/de8050cf-117b-46e6-b2de-329db8d2bfca%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/de8050cf-117b-46e6-b2de-329db8d2bfca=
%40isocpp.org</a>.<br />

------=_Part_1675_843545225.1476651862767--

------=_Part_1674_596928945.1476651862767--

.