Why auto_ptr_ref's constructor is not ``explicit''

  • Follow


At lease the implementations from G++ 4 and Visual C++ 2005 do not
declare std::auto_ptr_ref's constructor ``explicit''. e.g, in VC8, it's
like:

struct auto_ptr_ref {
    auto_ptr_ref(void* p) : p_(p) {};
    void * p_;
};

This could cause problem when using the auto_ptr like this:

    auto_ptr<A> pa = new A; // Wrong

The problem is, if use correctly, like:
    auto_ptr<A> pb(new A);
    auto_ptr<A> pa(pb); // OK

  ``pa'' will use ``auto_ptr::auto_ptr(auto_ptr_ref   temp)'' to
construct it's self,
while ``temp'' will come from an convertion of ``pb'' througn
``auto_ptr::operator auto_ptr_ref()''

But if use it like the first example, ``temp'' will try to construct
it's seft from the raw pointer ``A*'', in which case, it is actually
expecting a pointer-to-raw-pointer. dereference it will cause  memory
voilation.

Declare the constructor ``explicit auto_ptr_ref(void *p)'' could
prevent this kind of error, and this is why we have ``explicit''
keyword.

Does anyone know why it's not used in these implementations?

Thanks.
Jianyuan Wu
_________________
http://main.cc


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply return (3) 6/9/2006 9:02:23 AM

"defines main" <return@main.cc> wrote in message 
news:1149807094.676585.121320@u72g2000cwu.googlegroups.com...

> At lease the implementations from G++ 4 and Visual C++ 2005 do not
> declare std::auto_ptr_ref's constructor ``explicit''. e.g, in VC8, it's
> like:
>
> struct auto_ptr_ref {
>    auto_ptr_ref(void* p) : p_(p) {};
>    void * p_;
> };
>
> This could cause problem when using the auto_ptr like this:
>
>    auto_ptr<A> pa = new A; // Wrong
>
> The problem is, if use correctly, like:
>    auto_ptr<A> pb(new A);
>    auto_ptr<A> pa(pb); // OK
>
>  ``pa'' will use ``auto_ptr::auto_ptr(auto_ptr_ref   temp)'' to
> construct it's self,
> while ``temp'' will come from an convertion of ``pb'' througn
> ``auto_ptr::operator auto_ptr_ref()''
>
> But if use it like the first example, ``temp'' will try to construct
> it's seft from the raw pointer ``A*'', in which case, it is actually
> expecting a pointer-to-raw-pointer. dereference it will cause  memory
> voilation.
>
> Declare the constructor ``explicit auto_ptr_ref(void *p)'' could
> prevent this kind of error, and this is why we have ``explicit''
> keyword.
>
> Does anyone know why it's not used in these implementations?

Because I didn't know it was a problem until recently. (It's not
needed by EDG to get the proper behavior.) Once a helpful customer
told me that adding explicit solved this ugly problem with VC++,
I added it to our code base and informed Microsoft.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply P 6/10/2006 7:40:21 PM


1 Replies
238 Views

(page loaded in 0.035 seconds)

Similiar Articles:













7/30/2012 12:19:41 AM


Reply: