For Programmers: Free Programming Magazines  


Home > Archive > VC STL > February 2006 > Re: possiblity to assign any arbitrary pointer to a specialized auto_ptr in vs2005 (B









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: possiblity to assign any arbitrary pointer to a specialized auto_ptr in vs2005 (B
Ulrich Eckhardt

2006-02-13, 4:01 am

ms@nogga.de wrote:
> is it only me, or is it really possible to assign any arbitrary pointer
> to a specialized auto_ptr? E.g. the following code will compile in
> vs2005 with any problems:
>
> // assigning a pointer in the constructor
> std::auto_ptr<CDerived> test1 (new int);
> std::auto_ptr<CDerived> test2 (new double);
>
> // assigning a pointer in the copy constructor
> std::auto_ptr<CDerived> test3 = new int;
> std::auto_ptr<CDerived> test4 = new double;


Just for your info, both of the above should not compile, the first couple
because of a type mismatch the second one also because auto_ptr has an
explicit ctor.

> The reason for this behavior is the implicit conversion via the
> auto_ptr_ref class, which casts away the type information into a void *
> and from there back into the concrete type in the auto_ptr classes again.
>
> template<class _Ty>
> struct auto_ptr_ref
> { // proxy reference for auto_ptr copying
> auto_ptr_ref(void *_Right)
> : _Ref(_Right)
> { // construct from generic pointer to auto_ptr ptr
> }
>
> void *_Ref; // generic pointer to auto_ptr ptr
> };


I can't fathom the reasons for that, but two things strike me here:
- the ctor is not explicit
- use of a void pointer even though the exact type is known
I'd further check if this still works for cv qualified auto_ptrs, though I'm
not sure it's supposed to...

> I came across this problem in the following code:
>
> auto_ptr <CBase> MakeClass ()
> {
> return new CDerived ();
> }


This is ill-formed, too, because auto_ptr's ctor is explicit (it should
be...) so you have to explicitly create the auto_ptr.

Uli

Carl Daniel [VC++ MVP]

2006-02-13, 7:05 pm

ms@nogga.de wrote:
> As I mentioned, the problem is, that the code compiled and did run ok
> on VS6, it was a compile error on VS7 and VS7.1. In VS8 it compiles
> again but produces a runtime error.


I'd suggest that you open a bug report:

http://lab.msdn.microsoft.com/productfeedback

If you do so, please post a link to the bug report here so that others
following this thread can add their votes to it.

-cd


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com