Topic: auto_ptr<const T>
Author: "Dave Abrahams" <abrahams@mediaone.net>
Date: 1999/09/30 Raw View
In article <7stqkt$jjd$1@nnrp1.deja.com> , Alyena Jeni <alyena@my-deja.com>
wrote:
>
>
>
> As far as I know, the following code is perfectly legal:
>
> | #include <memory>
> | struct object
> | {
> | };
> |
> | int
> | main( int argc, char* argv[] )
> | {
> | std::auto_ptr<const object> ptr( new object );
> | return 0;
> | }
>
> However, STLPort 3.2.1 makes impossible to write such code -
> it uses
>
> | struct __auto_ptr_base {
> | void* _M_p;
> | };
>
> as base class for
>
> | template<class T>
> | struct auto_ptr;
>
> and the standard not allows implicit conversion from const int*
> to void* (4.10.2) in the auto_ptr constructor:
>
> | explicit auto_ptr(_Tp* __px) __STL_NOTHROW { _M_p = __px; }
>
> my questions are:
> 1) am I right or I missed something?
You're correct; you've found a bug in the STLPort implementation of
auto_ptr. A fix has been posted to the STLPort maintainer.
> 2) is it legal to fix problem with the following code:
>
> | template<class T>
> | T*
> | __remove_const( const T* ptr )
> | {
> | return const_cast<T*>( ptr );
> | }
> |
> | explicit auto_ptr(_Tp* __px) __STL_NOTHROW
> | { _M_p = __remove_const( __px ); }
I assume you also have
template<class T> T* __remove_const(T* ptr) { return ptr; }
without which your fix will fail for auto_ptr<non-const T>
While conscientious, the use of __remove_const is overkill. A few
bad-ol'-C-style casts to (void*) will do the trick. You'll need one in each
constructor and one in the reset() function.
> My compiler (MSVC 6.0) also prohibits deletion through const pointer,
> so I have to rewrite auto_ptr destructor as
>
> | ~auto_ptr() { delete __remove_const( get() ); }
>
Ah. Well, there's no accounting for incorrect compilers.
-Dave
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alyena Jeni <alyena@my-deja.com>
Date: 1999/09/29 Raw View
As far as I know, the following code is perfectly legal:
| #include <memory>
| struct object
| {
| };
|
| int
| main( int argc, char* argv[] )
| {
| std::auto_ptr<const object> ptr( new object );
| return 0;
| }
However, STLPort 3.2.1 makes impossible to write such code -
it uses
| struct __auto_ptr_base {
| void* _M_p;
| };
as base class for
| template<class T>
| struct auto_ptr;
and the standard not allows implicit conversion from const int*
to void* (4.10.2) in the auto_ptr constructor:
| explicit auto_ptr(_Tp* __px) __STL_NOTHROW { _M_p = __px; }
my questions are:
1) am I right or I missed something?
2) is it legal to fix problem with the following code:
| template<class T>
| T*
| __remove_const( const T* ptr )
| {
| return const_cast<T*>( ptr );
| }
|
| explicit auto_ptr(_Tp* __px) __STL_NOTHROW
| { _M_p = __remove_const( __px ); }
My compiler (MSVC 6.0) also prohibits deletion through const pointer,
so I have to rewrite auto_ptr destructor as
| ~auto_ptr() { delete __remove_const( get() ); }
:(
--
Alyena
Sent via Deja.com http://www.deja.com/
Before you buy.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]