Article View: comp.std.c++
Article #1926Re: Add dynamic_cast<type_info&>
From: Fergus Henderson
Date: Fri, 08 Sep 1995 18:21
Date: Fri, 08 Sep 1995 18:21
65 lines
3394 bytes
3394 bytes
Etay Bogner <Etay_Bogner@mail.stil.scitex.com> writes:
>fjh@munta.cs.mu.OZ.AU (Fergus Henderson) wrote:
>
>>> If you were proposing to define
>>> semantics for `dynamic_cast<bar>(foo)', what would be the static type
>>> of that expression? What would be the behaviour at run-time?
[...]
>A 'const type_info&' is stored somewhere, and I want to use this data to
>try and cast a pointer ( or a reference ) to the type that is represented
>by the stored type_info.
[...]
>So, the conclusion from this SHORT study-case, is that the STATIC type in
>the expression 'dynamic_cast<T*>(p)' is NOT a static type at all.
>
>From here, it's easy to understand that the STATIC RETURN TYPE of the
>exspression 'dynamic_cast<T*>(p)' should be T*, as it is now.
I already know that the static type of `dynamic_cast<T*>(p)' is `T*',
because `dynamic_cast<T*>(p)' is already part of standard C++.
What I am asking about is the static type of your newly proposed syntax
`dynamic_cast<bar>(foo)', where `bar' is a value of type type_info, not
a type.
I think your idea doesn't make sense, because there *isn't* any sensible
static type for that expression. Since C++ is a statically typed language,
every expression must have a static type, but that expression can't have
one. Therefore it doesn't work in C++.
>So, what I'm actually saying is that if it's possible to perform the
>dynamic_cast without knowing the T being used, why not do so ?
Because in fact it is not possible, unless the result of the dynamic_cast
is discarded. The reason the compiler needs to know the type is
so that it can statically typecheck the containing expression.
>My suggestion is that if the compiler encounters an expression like :
>
>'dynamic_cast<ATypeInfo>(p)' were ATypeInfo is a 'const type_info&'
>
>the compiler won't try to cast p to ATypeInfo, which will ALWAYS fail, but
>it will try to cast p to the type ATypeInfo contains data for.
And supposing the compiler encounters an expression like
int f(const type_info& ATypeInfo) {
int x = dynamic_cast<ATypeInfo>(p);
return x;
}
what is the compiler going to do then? Type-check the initialization
of `x' at run-time? I suppose that would be do-able, but it wouldn't
be easy, and it certainly wouldn't be C++.
--
Fergus Henderson | #define x t=a[i],a[i]=a[m],a[m]=t
| char a[]=" 12345678";main(m,i,j,t){for(i=m;i<
fjh@cs.mu.oz.au | 9;x,i++)for(x,j=m;--j?(t=a[m-j]-a[m])-j&&t+j:
http://www.cs.mu.oz.au/~fjh | main(m+1)*0;);m-9||puts(a+1);} /* 8 queens */
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<MATT.95Sep8112137@physics2.Berkeley.EDU>Path:
arc1-ca.pugleaf.net!128.32.155.1!mattReferences:
<9508048102.AA810263857@mail.stil.scitex.com> <MATT.95Sep7112147@physics10.Berkeley.EDU> <Etay_Bogner-0809950030420001@applepo11.scitex.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson) Newsgroups: comp.std.c++ Subject: Re: Add dynamic_cast<type_info&> Date: 08 Sep 1995 18:21:37 GMT Organization: Computer Science, University of Melbourne, Australia Approved: matt@physics2.Berkeley.EDU (Matt Austern) Message-ID: <MATT.95Sep8112137@physics2.Berkeley.EDU> References: <9508048102.AA810263857@mail.stil.scitex.com> <MATT.95Sep7112147@physics10.Berkeley.EDU> <Etay_Bogner-0809950030420001@applepo11.scitex.com> NNTP-Posting-Host: physics2.berkeley.edu