|
|
Why auto_ptr_ref's constructor is not ``explicit''
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: explicit calling of member template constructor... - comp.lang.c++ ...Hi All, I have this example that attempts to call a member template constructor whilst explicitly specifying the template parameters. I realize t... Swing uses bad features - comp.lang.java.gui... variables are created before super constructor > called. > But why do you need explicit ... by all methods, hold; prior to the constructor's invocation there may not ... C++0X explicitly defaulted copy constructors - comp.lang.c++ ...The Standard requires that such an explicit-defaulting must match the ... 0x concepts even make that easier, while fixing its limits (it's not possible to check constructors ... Const constructor - comp.lang.c++.moderatedIt's not safe. That's why iterator and const_iterator of containers are separate types ... struct with const member variable without explicit constructor ... Hi. Is the ... Multiple definition and specialization of static data member ...Why? It's not problematic at ... implementation's problem, however, and not yours ; the same thing holds for the constructor ... and not a declaration - of the explicit ... nitializing a static vector <> of integers (this static vectorDo I in the encapsulating class constructor call std::vector<T>::reserve (31), since ... please tell us about it because when WG21 made the contiguity requirement explicit ... STL allocators, global new/delete using the heap and shared memory ...It prints out 'I am being used!' when the allocator's alloc function is ... struct with const member variable without explicit constructor ... STL allocators, global new ... ptr versus const ptr& - comp.lang.c++.moderatedAn explicit constructor makes it mandatory to take according actions, i.e. increment the refcounter in advance so that the ptr<X> doesn't reach zero. naming convention for class attributes (member data)? - comp.lang ...> > It's not out of the realm of possibility that you are simply being ... struct with const member variable without explicit constructor ... naming convention for ... When would you declare a method as static? - comp.lang.java.help ...> I think a good example is Java's Math class ... class , doubling as its own constructor and with ... the creation of the activation record explicit. I was not saying ... BUG? auto_ptr assignment crash in VS2005 - Microsoft Corporation ...//This code compiles in VS 2005 and couldn't be compiled in gcc 3.4.2, //because auto_ptr_ref constructor is made explicit there, but not in visual studio. Constructor (object-oriented programming) - Wikipedia, the free ...Constructors never have an explicit return type. Constructors cannot be ... However, when a constructor is not defined, the next one found in the class's Method Resolution ... 7/30/2012 12:19:41 AM
|
|
|
|
|
|
|
|
|