Topic: generic dynamic / runtime cast (cast to real runtime type)


Author: "'David Feurle' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Sun, 16 Oct 2016 09:10:59 -0700 (PDT)
Raw View
------=_Part_159_1569755242.1476634259314
Content-Type: multipart/alternative;
 boundary="----=_Part_160_1162686755.1476634259315"

------=_Part_160_1162686755.1476634259315
Content-Type: text/plain; charset=UTF-8

Hello,

I like the works of C# dynamic which casts a variable to it's real runtime
type and I would like to see a similar feature in C++.

Example in C#:

Object o = new String("hahaha");
dynamic d = o;                                    // the type of d is now
string
d += "haha";                                        //append something to
the string

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.
If the dynamic where initialized with a different object of a different
type (for example int) the code would need to use the operators of the
runtime type.
The runtime types are not known at compile time -> the code needs to be
"recompiled" at runtime according to the runtime type.

This is not possible in C++. As a workaround the possible types of the
runtime_cast could be given as a template parameter.
The code that uses the dynamic variable could be compiled  since all
candidates are known at compiletime.
Rtti could be used to determine the type at runtime.

Example in C++:

struct Base {
    virtual Base() = default;
};

struct A : public Base { void fun(); };
struct B : public Base { void fun(); };

fun(A*);
fun(B*);

Base* b = new A();
auto dyn = runtime_cast<A*, B*>(b);   // the type of dyn is either A* or B*
if (!dyn) { return; }                                 //the type of b was
neither A* or B*
dyn->fun();                                           // either A::fun is
called or B::fun
                                                             //all possible
types of dyn are known at compiletime and code could be generated for each
one of it.
fun(dyn);                                               //overload
resolution is triggered here according to the runtime type of dyn

The code using the variable dyn needs to be compiled with all possible
types of it. At runtime the type of the object is used to decide which
concrete implementation is called.
Using this feature would enable us to completele obsolete the visitor
pattern and trigger overload resolution based on a runtime type.

What do you think about this?

Regards,

David Feurle


--
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/3d2ec365-84af-40e2-aa01-2807739716f8%40isocpp.org.

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

<div dir=3D"ltr">Hello,<div><br></div><div>I like the works of C# dynamic w=
hich casts a variable to it&#39;s real runtime type and I would like to see=
 a similar feature in C++.</div><div><br></div><div>Example in C#:</div><di=
v><br></div><div>Object o =3D new String(&quot;hahaha&quot;);</div><div>dyn=
amic 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 ty=
pe of d is now string</div><div>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 the s=
tring</div><div><br></div><div>As you can see the dynamic variable will be =
of the runtime type of it&#39;s content. The following code uses the operat=
ors of string.</div><div>If the dynamic where initialized with a different =
object of a different type (for example int) the code would need to use the=
 operators of the runtime type.</div><div>The runtime types are not known a=
t compile time -&gt; the code needs to be &quot;recompiled&quot; at runtime=
 according to the runtime type.</div><div><br>This is not possible in C++. =
As a workaround the possible types of the runtime_cast could be given as a =
template parameter.</div><div>The code that uses the dynamic variable could=
 be compiled =C2=A0since all candidates are known at compiletime.</div><div=
>Rtti could be used to determine the type at runtime.</div><div><br></div><=
div>Example in C++:</div><div><br></div><div>struct Base {<br>=C2=A0 =C2=A0=
 virtual Base() =3D default;</div><div>};</div><div><br></div><div>struct A=
 : public Base { void fun(); };<br></div><div>struct B : public Base { void=
 fun(); };</div><div><br></div><div>fun(A*);</div><div>fun(B*);</div><div><=
br></div><div>Base* b =3D new A();<br>auto dyn =3D runtime_cast&lt;A*, B*&g=
t;(b); =C2=A0 // the type of dyn is either A* or B*<br></div><div>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 neith=
er A* or B*</div><div>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 called or B::fun<br>=
=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 types of dyn are known at compiletime and code could be generated =
for each one of it.</div><div>fun(dyn); =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 //overload resolution =
is triggered here according to the runtime type of dyn</div><div><br></div>=
<div>The code using the variable dyn needs to be compiled with all possible=
 types of it. At runtime the type of the object is used to decide which con=
crete implementation is called.</div><div>Using this feature would enable u=
s to completele obsolete the visitor pattern and trigger overload resolutio=
n based on a runtime type.</div><div><br></div><div>What do you think about=
 this?</div><div><br></div><div>Regards,</div><div><br></div><div>David Feu=
rle</div><div><br></div><div><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/3d2ec365-84af-40e2-aa01-2807739716f8%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/3d2ec365-84af-40e2-aa01-2807739716f8=
%40isocpp.org</a>.<br />

------=_Part_160_1162686755.1476634259315--

------=_Part_159_1569755242.1476634259314--

.