Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Deep Copy smart pointer not requiring virtual copy constructor

Reply
Thread Tools

Deep Copy smart pointer not requiring virtual copy constructor

 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-14-2006
Nindi wrote:

>
> Kai-Uwe Bux wrote:
>> Posts are getting too long. Let's separate issues.
>>
>> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>>
>> >> template < typename T >
>> >> struct non_polymorphic_class_error {
>> >> enum {
>> >> trigger = sizeof( dynamic_cast<void*>( static_cast<T*>(0) ) )
>> >> };
>> >>
>> >> };
>> >>
>> >
>> > I forbid myslef from using dynamic_cast, but I would have to try to
>> > remeber the reasons why. But I do have a question.
>> > Isn't it true that dynamic_cast happens at runtime and therefore we
>> > have a overhead, whereas static_cast takes place at compile time, so
>> > we don't. I think the boost is_base combined with boost enable_if lets
>> > us 'error check' at compile time.

>>
>> If you use
>>
>> non_polymorphic_class_error< Base > dummy;
>>
>> then there is a dummy variable that is never used. The compiler will
>> optimize that away. The dynamic_cast, however, will not be evaluated
>> since it sits within a sizeof expression. Those are not evaluated,
>> instead the compiler figures out the correct value magically.
>>
>> Maybe the best way to check is just to put the line
>>
>> sizeof( dynamic_cast<void*>( static_cast<Base*>(0) ) );
>>
>> somewhere. It evaluates to a const-expression without side-effects and
>> therefore should not create any code.

> I am not sure how it can do this, isn't dynamic_cast a runtime
> evaluation ?


So what? The standard says [5.2.7/5-6]:

...
Otherwise, v shall be a pointer to or an lvalue of a polymorphic
type (10.3).

The crucial thing here is the word "shall". It makes this provision into a
diagnosable rule, i.e., since the compiler knows at compile time whether
the argument to dynamic_cast has static type "pointer to polymorphic" the
compiler is required to issue a diagnostic if the provision is violated.
Try:

struct NonPolymorphic {};

int main ( void ) {
sizeof( dynamic_cast<void*>( static_cast<NonPolymorphic*>(0) ) );
}

You should get a *compile time* error on this one.

On the other hand

struct Polymorphic {

virtual
~Polymorphic ( void ) {}

};

int main ( void ) {
sizeof( dynamic_cast<void*>( static_cast<Polymorphic*>(0) ) );
}

compiles fine. Now, you may worry about runtime overhead. However, since we
embedded the cast within a sizeof expression, we can be sure that it will
not be evaluated. Instead, it is replaced by its result through compiler
magic (i.e., the result is found at compile time).



Best

Kai-Uwe Bux

 
Reply With Quote
 
 
 
 
Nindi
Guest
Posts: n/a
 
      11-14-2006

Kai-Uwe Bux wrote:
> Nindi wrote:
>
> >
> > Kai-Uwe Bux wrote:
> >> Posts are getting too long. Let's separate issues.
> >>
> >> (E-Mail Removed) wrote:
> >>
> >> >> template < typename T >
> >> >> struct non_polymorphic_class_error {
> >> >> enum {
> >> >> trigger = sizeof( dynamic_cast<void*>( static_cast<T*>(0) ) )
> >> >> };
> >> >>
> >> >> };
> >> >>
> >> >
> >> > I forbid myslef from using dynamic_cast, but I would have to try to
> >> > remeber the reasons why. But I do have a question.
> >> > Isn't it true that dynamic_cast happens at runtime and therefore we
> >> > have a overhead, whereas static_cast takes place at compile time, so
> >> > we don't. I think the boost is_base combined with boost enable_if lets
> >> > us 'error check' at compile time.
> >>
> >> If you use
> >>
> >> non_polymorphic_class_error< Base > dummy;
> >>
> >> then there is a dummy variable that is never used. The compiler will
> >> optimize that away. The dynamic_cast, however, will not be evaluated
> >> since it sits within a sizeof expression. Those are not evaluated,
> >> instead the compiler figures out the correct value magically.
> >>
> >> Maybe the best way to check is just to put the line
> >>
> >> sizeof( dynamic_cast<void*>( static_cast<Base*>(0) ) );
> >>
> >> somewhere. It evaluates to a const-expression without side-effects and
> >> therefore should not create any code.

> > I am not sure how it can do this, isn't dynamic_cast a runtime
> > evaluation ?

>
> So what? The standard says [5.2.7/5-6]:
>
> ...
> Otherwise, v shall be a pointer to or an lvalue of a polymorphic
> type (10.3).
>
> The crucial thing here is the word "shall". It makes this provision into a
> diagnosable rule, i.e., since the compiler knows at compile time whether
> the argument to dynamic_cast has static type "pointer to polymorphic" the
> compiler is required to issue a diagnostic if the provision is violated.
> Try:
>
> struct NonPolymorphic {};
>
> int main ( void ) {
> sizeof( dynamic_cast<void*>( static_cast<NonPolymorphic*>(0) ) );
> }
>
> You should get a *compile time* error on this one.
>
> On the other hand
>
> struct Polymorphic {
>
> virtual
> ~Polymorphic ( void ) {}
>
> };
>
> int main ( void ) {
> sizeof( dynamic_cast<void*>( static_cast<Polymorphic*>(0) ) );
> }
>
> compiles fine. Now, you may worry about runtime overhead. However, since we
> embedded the cast within a sizeof expression, we can be sure that it will
> not be evaluated. Instead, it is replaced by its result through compiler
> magic (i.e., the result is found at compile time).
>
>
>


I see. I'll have a play about with it.
Thanks
N

 
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
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
Smart Pointer showing deep copy semantics arijit79@gmail.com C++ 2 12-25-2008 03:43 PM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM



Advertisments