Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast?

Reply
Thread Tools

Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast?

 
 
Pavel
Guest
Posts: n/a
 
      09-17-2010
Let us assume that A and B are classes, the variable aPtr has type A*,
and the code containing the following assert statement compiles without
error:

assert(!dynamic_cast<B*>(aPtr)
|| (dynamic_cast<B*>(aPtr) == static_cast<B*>(aPtr)));


Can A and B be defined in a way that the assertion would fail?

TIA,
-Pavel

 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      09-17-2010
Pavel wrote:
> Let us assume that A and B are classes, the variable aPtr has type A*,
> and the code containing the following assert statement compiles without
> error:
>
> assert(!dynamic_cast<B*>(aPtr)
> || (dynamic_cast<B*>(aPtr) == static_cast<B*>(aPtr)));
>
>
> Can A and B be defined in a way that the assertion would fail?



AFAIK no, since the first condition checks whether a dynamic cast is
allowed.


Marcel
 
Reply With Quote
 
 
 
 
Vaclav Haisman
Guest
Posts: n/a
 
      09-17-2010
Pavel wrote, On 17.9.2010 13:33:
> Let us assume that A and B are classes, the variable aPtr has type A*, and
> the code containing the following assert statement compiles without error:
>
> assert(!dynamic_cast<B*>(aPtr)
> || (dynamic_cast<B*>(aPtr) == static_cast<B*>(aPtr)));
>
>
> Can A and B be defined in a way that the assertion would fail?

The following gives me false with codepad.org:

#include <iostream>

struct A
{
virtual ~A ()
{ }
};

struct C
{
virtual ~C ()
{ }
};

struct B : C, A
{ };

struct E : A
{ };

struct D : E, B
{ };

int
main ()
{
D d;
A * aPtr = static_cast<E *>(&d);
std::cout << (dynamic_cast<B*>(aPtr) == static_cast<B*>(aPtr));
}


--
VH
 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      09-17-2010
Vaclav Haisman wrote:
> The following gives me false with codepad.org:
>
> #include <iostream>
>
> struct A
> {
> virtual ~A ()
> { }
> };
>
> struct C
> {
> virtual ~C ()
> { }
> };
>
> struct B : C, A
> { };
>
> struct E : A
> { };
>
> struct D : E, B
> { };
>
> int
> main ()
> {
> D d;
> A * aPtr = static_cast<E *>(&d);
> std::cout << (dynamic_cast<B*>(aPtr) == static_cast<B*>(aPtr));
> }


This code has undefined behavior. static_cast<B*>(aPtr) is invalid
because the instance of A in aPtr is not a superclass of type B. It is a
superclass of type E instead.

Of course, dynamic_cast does not fail on this cross cast.


Marcel
 
Reply With Quote
 
Pavel
Guest
Posts: n/a
 
      09-18-2010
Vaclav Haisman wrote:
> Pavel wrote, On 17.9.2010 13:33:
>> Let us assume that A and B are classes, the variable aPtr has type A*, and
>> the code containing the following assert statement compiles without error:
>>
>> assert(!dynamic_cast<B*>(aPtr)
>> || (dynamic_cast<B*>(aPtr) == static_cast<B*>(aPtr)));
>>
>>
>> Can A and B be defined in a way that the assertion would fail?

> The following gives me false with codepad.org:
>
> #include<iostream>
>
> struct A
> {
> virtual ~A ()
> { }
> };
>
> struct C
> {
> virtual ~C ()
> { }
> };
>
> struct B : C, A
> { };
>
> struct E : A
> { };
>
> struct D : E, B
> { };
>
> int
> main ()
> {
> D d;
> A * aPtr = static_cast<E *>(&d);
> std::cout<< (dynamic_cast<B*>(aPtr) == static_cast<B*>(aPtr));
> }
>
>

Thanks so much Vaclav!

I felt something was fishy in that assumption in the code I was reading
but could not exactly understand what was that.

-Pavel

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-18-2010
On Sep 17, 12:33 pm, Pavel
<(E-Mail Removed)> wrote:
> Let us assume that A and B are classes, the variable aPtr has type A*,
> and the code containing the following assert statement compiles without
> error:


> assert(!dynamic_cast<B*>(aPtr)
> || (dynamic_cast<B*>(aPtr) == static_cast<B*>(aPtr)));


> Can A and B be defined in a way that the assertion would fail?


I don't think so off hand, but it's certainly possible to define
the classes in such a way that the static_cast won't be legal
(although the dynamic_cast is).

--
James Kanze
 
Reply With Quote
 
Vaclav Haisman
Guest
Posts: n/a
 
      09-18-2010
Marcel Müller wrote, On 17.9.2010 22:39:
> Vaclav Haisman wrote:
>> The following gives me false with codepad.org:
>>
>> #include <iostream>
>>
>> struct A
>> {
>> virtual ~A ()
>> { }
>> };
>>
>> struct C
>> {
>> virtual ~C ()
>> { }
>> };
>>
>> struct B : C, A
>> { };
>>
>> struct E : A
>> { };
>>
>> struct D : E, B
>> { };
>>
>> int
>> main ()
>> {
>> D d;
>> A * aPtr = static_cast<E *>(&d);
>> std::cout << (dynamic_cast<B*>(aPtr) == static_cast<B*>(aPtr));
>> }

>
> This code has undefined behavior. static_cast<B*>(aPtr) is invalid because
> the instance of A in aPtr is not a superclass of type B. It is a superclass
> of type E instead.
>
> Of course, dynamic_cast does not fail on this cross cast.

I did not realise it was UB but it does make sense. Though that's maybe what
the assert is about, possibly detecting UB, however fragile/undefined it
might be.

--
VH
 
Reply With Quote
 
Pavel
Guest
Posts: n/a
 
      09-18-2010
James Kanze wrote:
> On Sep 17, 12:33 pm, Pavel
> <(E-Mail Removed)> wrote:
>> Let us assume that A and B are classes, the variable aPtr has type A*,
>> and the code containing the following assert statement compiles without
>> error:

>
>> assert(!dynamic_cast<B*>(aPtr)
>> || (dynamic_cast<B*>(aPtr) == static_cast<B*>(aPtr)));

>
>> Can A and B be defined in a way that the assertion would fail?

>
> I don't think so off hand, but it's certainly possible to define
> the classes in such a way that the static_cast won't be legal
> (although the dynamic_cast is).
>
> --
> James Kanze

Thanks James! Vaclav gave a good example in his above post. I tend to
forget about a possibility to have more than one identical base class
because I don't use it often. I just remembered the code should not have
relied on the above condition in general but could not recall why.

-Pavel
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Internet Sharing: Equal upload speeds but un-equal download speeds =?Utf-8?B?TkpU?= Wireless Networking 3 09-15-2007 06:22 AM
why (getchar() != EOF) always equal to 1 arnuld C Programming 12 03-09-2007 11:08 PM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM
Does sizeof(char) always equal to 1? Sunner Sun C Programming 9 05-23-2005 02:43 AM
Does a static variable in a class's member fn always remain static? Sam C++ 4 01-13-2004 11:05 PM



Advertisments