Topic: Half static / half dynamic _cast


Author: nagle@animats.com (John Nagle)
Date: Mon, 29 Nov 2004 18:58:20 GMT
Raw View
Bob Hairgrove wrote:

> On Sat, 27 Nov 2004 23:51:21 GMT, Nicola.Musatti@ObjectWay.it (Nicola
> Musatti) wrote:
>
>
>>Hallo,
>>If my understanding of the standard is correct, dynamic_cast performs
>>all checks related to the actual convertibility at runtime. However
>>all the information required to check whether the conversion is
>>possible is available at compile time. Wouldn't it be more helpful for
>>developers to have such a check be required at compile time?

    Certainly dynamic cast can be optimized.  One would expect
this dynamic cast to be optimized out.

 class A;
 A* p1 = new A;
 ....
 A* p2 = dynamic_cast<A*>(p1);

    Template-generated code often creates situations like that,
where something is effectively a no-op.  Such cases should
be optimized out whenever possible.

    John Nagle
    Animats

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Nicola.Musatti@ObjectWay.it (Nicola Musatti)
Date: Tue, 30 Nov 2004 03:41:27 GMT
Raw View
nospam@nospam.ucar.edu ("Thomas Mang") wrote in message news:<41a9904b$0$11872$3b214f66@usenet.univie.ac.at>...
> "Nicola Musatti" <Nicola.Musatti@ObjectWay.it> schrieb im Newsbeitrag
> news:a327cf48.0411250351.3c25290@posting.google.com...
> > Hallo,
> > If my understanding of the standard is correct, dynamic_cast performs
> > all checks related to the actual convertibility at runtime. However
> > all the information required to check whether the conversion is
> > possible is available at compile time.
>
> Not really. dynamic_cast can also perform cross-casts.

So? The standard requires both the argument and the result types to be
complete. I can't see how that can be without exposing also the
relationship (or lack thereof) between the two.

Cheers,
Nicola Musatti

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: news@news.astraweb.com ("News Subsystem")
Date: Tue, 30 Nov 2004 03:42:55 GMT
Raw View
On Sat, 27 Nov 2004 23:51:21 GMT, Nicola Musatti wrote:

> Hallo,
> If my understanding of the standard is correct, dynamic_cast performs
> all checks related to the actual convertibility at runtime. However
> all the information required to check whether the conversion is
> possible is available at compile time. Wouldn't it be more helpful for
> developers to have such a check be required at compile time?

This code is incomplete and untested but should give the idea:

class A {};
class B1 : public A {};
class B2 : public A {};

void f (A *a)
{
 B1 *b = dynamic_cast<B1*> (a);
}

int main ()
{
 int opt;
 std::cin >> opt;
 A *a;
 if (opt == 1)
  a = new B1();
 else
  a = new B2();
 f(a);
}

How can the compiler know whether or not the dynamic_cast in f will
succeed?  There isn't enough information until run-time.

--
Greg Schmidt  gregs@trawna.com
  Trawna Publications  http://www.trawna.com/

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Tue, 30 Nov 2004 15:11:18 GMT
Raw View
Nicola Musatti wrote:
> nospam@nospam.ucar.edu ("Thomas Mang") wrote in message news:<41a9904b$0$11872$3b214f66@usenet.univie.ac.at>...
>
>>Not really. dynamic_cast can also perform cross-casts.
>
> So? The standard requires both the argument and the result types to be
> complete. I can't see how that can be without exposing also the
> relationship (or lack thereof) between the two.

So? Frankly, I fail to see your point. A lot of people, including
myself, have proven you that *in the general case* the information
required to perform the dynamic_cast is *not* available at compile-time.
I hope you'll agree at least on this point.

Of course, it may happen in specific cases, as in John Nagle's example,
that the information is indeed available. Are you suggesting that in
those *specific cases* the compiler should be allowed/required to use
that extra information to perform the cast statically?

Alberto

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jdennett@acm.org (James Dennett)
Date: Tue, 30 Nov 2004 18:26:19 GMT
Raw View
Nicola Musatti wrote:

> nospam@nospam.ucar.edu ("Thomas Mang") wrote in message news:<41a9904b$0$11872$3b214f66@usenet.univie.ac.at>...
>
>>"Nicola Musatti" <Nicola.Musatti@ObjectWay.it> schrieb im Newsbeitrag
>>news:a327cf48.0411250351.3c25290@posting.google.com...
>>
>>>Hallo,
>>>If my understanding of the standard is correct, dynamic_cast performs
>>>all checks related to the actual convertibility at runtime. However
>>>all the information required to check whether the conversion is
>>>possible is available at compile time.
>>
>>Not really. dynamic_cast can also perform cross-casts.
>
>
> So? The standard requires both the argument and the result types to be
> complete. I can't see how that can be without exposing also the
> relationship (or lack thereof) between the two.

struct A { virtual ~A() {} };
struct B { virtual ~B() {} };
// No relationship between A and B so far

struct C : A, B { };
// But not there is a relationship between A and B

int main() {
   A* a = new C;
   // dynamic_cast can find the relationship
   delete dynamic_cast<B*>(a);
}

-- James

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: nospam@nospam.ucar.edu ("Thomas Mang")
Date: Tue, 30 Nov 2004 18:26:33 GMT
Raw View
"Nicola Musatti" <Nicola.Musatti@ObjectWay.it> schrieb im Newsbeitrag
news:a327cf48.0411290538.2ddff293@posting.google.com...
> nospam@nospam.ucar.edu ("Thomas Mang") wrote in message
news:<41a9904b$0$11872$3b214f66@usenet.univie.ac.at>...
> > "Nicola Musatti" <Nicola.Musatti@ObjectWay.it> schrieb im Newsbeitrag
> > news:a327cf48.0411250351.3c25290@posting.google.com...
> > > Hallo,
> > > If my understanding of the standard is correct, dynamic_cast performs
> > > all checks related to the actual convertibility at runtime. However
> > > all the information required to check whether the conversion is
> > > possible is available at compile time.
> >
> > Not really. dynamic_cast can also perform cross-casts.
>
> So? The standard requires both the argument and the result types to be
> complete. I can't see how that can be without exposing also the
> relationship (or lack thereof) between the two.


See the example given by Alberto Barbati.


Thomas


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Nicola.Musatti@ObjectWay.it (Nicola Musatti)
Date: Tue, 30 Nov 2004 18:26:41 GMT
Raw View
AlbertoBarbati@libero.it (Alberto Barbati) wrote in message news:<q2kqd.54879$Ni.1894836@twister1.libero.it>...
> Nicola Musatti wrote:
> > If my understanding of the standard is correct, dynamic_cast performs
> > all checks related to the actual convertibility at runtime. However
> > all the information required to check whether the conversion is
> > possible is available at compile time.
>
> Not quite. The feasibility of the conversion made by dynamic_cast
> depends on the dynamic type of the object, an information which is *not*
> available at compile time.

I stand corrected. However I still feel that a well behaved compiler
should warn me when the information that a conversion is not possible
is available.

Cheers,
Nicola Musatti

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: evans.paul@gmail.com (paul_evans)
Date: Fri, 3 Dec 2004 17:45:49 GMT
Raw View
Nicola.Musatti@ObjectWay.it (Nicola Musatti) wrote in message news:<a327cf48.0411300145.4a9a0772@posting.google.com>...
> AlbertoBarbati@libero.it (Alberto Barbati) wrote in message news:<q2kqd.54879$Ni.1894836@twister1.libero.it>...
> > Nicola Musatti wrote:
> > > If my understanding of the standard is correct, dynamic_cast performs
> > > all checks related to the actual convertibility at runtime. However
> > > all the information required to check whether the conversion is
> > > possible is available at compile time.
> >
> > Not quite. The feasibility of the conversion made by dynamic_cast
> > depends on the dynamic type of the object, an information which is *not*
> > available at compile time.
>
> I stand corrected. However I still feel that a well behaved compiler
> should warn me when the information that a conversion is not possible
> is available.

It's the other way around: a well behaved compiler could optimise away
the dynamic aspect of the cast when knows the conversion will always
succeed.

Cheers,
Paul

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Nicola.Musatti@ObjectWay.it (Nicola Musatti)
Date: Sat, 27 Nov 2004 23:51:21 GMT
Raw View
Hallo,
If my understanding of the standard is correct, dynamic_cast performs
all checks related to the actual convertibility at runtime. However
all the information required to check whether the conversion is
possible is available at compile time. Wouldn't it be more helpful for
developers to have such a check be required at compile time?

Cheers,
Nicola Musatti

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: nospam@nospam.ucar.edu ("Thomas Mang")
Date: Sun, 28 Nov 2004 22:20:14 GMT
Raw View
"Nicola Musatti" <Nicola.Musatti@ObjectWay.it> schrieb im Newsbeitrag
news:a327cf48.0411250351.3c25290@posting.google.com...
> Hallo,
> If my understanding of the standard is correct, dynamic_cast performs
> all checks related to the actual convertibility at runtime. However
> all the information required to check whether the conversion is
> possible is available at compile time.

Not really. dynamic_cast can also perform cross-casts.


Thomas


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Mon, 29 Nov 2004 17:55:37 GMT
Raw View
Nicola Musatti wrote:
> If my understanding of the standard is correct, dynamic_cast performs
> all checks related to the actual convertibility at runtime. However
> all the information required to check whether the conversion is
> possible is available at compile time.

Not quite. The feasibility of the conversion made by dynamic_cast
depends on the dynamic type of the object, an information which is *not*
available at compile time. For example:

class BaseA { /* polymorphic */ };
class BaseB { /* polymorphic */ };

void test(BaseA* pa)
{
   // the dynamic type of *pa is unknown at compile time
   // so the check must be performed at runtime
   // notice that BaseA and BaseB are unrelated classes
   BaseB* pb = dynamic_cast<BaseB*>(pA);

   /* ... */
}

class Derived : public BaseA, public BaseB { /* ... */ };

int main()
{
   test(new Derived);
   return 0;

}

---
Alberto

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: invalid@bigfoot.com (Bob Hairgrove)
Date: Mon, 29 Nov 2004 17:55:33 GMT
Raw View
On Sat, 27 Nov 2004 23:51:21 GMT, Nicola.Musatti@ObjectWay.it (Nicola
Musatti) wrote:

>Hallo,
>If my understanding of the standard is correct, dynamic_cast performs
>all checks related to the actual convertibility at runtime. However
>all the information required to check whether the conversion is
>possible is available at compile time. Wouldn't it be more helpful for
>developers to have such a check be required at compile time?
>
>Cheers,
>Nicola Musatti

But it's not always available at compile time ... that's the whole
point of dynamic_cast and RTTI. If I have a function which takes a
pointer or reference to the base class of a polymorphic object, there
is no way the compiler can know how the function is called at runtime.

Consider the very common case of function F taking a reference to base
class A being compiled separately and placed in a library. Assume that
at the time the library was built, base class A had known descendants
B and C. Inside F, dynamic_cast is used to determine whether or not
the A& is really a B& or a C&. After I build the library, I need new
classes D and E, derived from B or C, a year later. I should still be
able to call F and pass D or E by reference, and dynamic_cast<B&>
should still work on D& or E& inside of F.

--
Bob Hairgrove
NoSpamPlease@Home.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]