Topic: Why operator [] is not provided for auto_ptr in the standard C++ libaray?
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/01/12 Raw View
Ian Haggard wrote:
> [Moderator's note: this is a repost. When this article was posted
> previously, it somehow got mutilated: the five lines beginning with
> "...", including the author's signature, were omitted. mha]
I had problems understanding the first post !
> In article <01bbfab9$d2f13ee0$a10f9aa5@albert>,
> "Anson Tsao" <ansont@hookup.net> wrote:
> >
> > One more auto_ptr question:
> >
> > Why doesn't auto_ptr implement
> > template class<T> T* auto_ptr<T>::operator T*() ?
> > Would be more readable than using auto_ptr::get();
>
> Scott Meyers has some excellent sections dealing with auto_ptr and other
> types of smart pointers in "More Effective C++". He also has a section
> which talks about the problems that can be caused by user-defined
> conversions. If you really want the long answer to this and lots of
> other questions you may have about auto_ptr and other smart pointers,
> I'd recommend reading his book. BTW, he also has some updated
> information about auto_ptr on-line at http://www.aw.com/cp/mec++.html
>
> The short answer is that having and operator T*() would make possible
> travesties such as this:
>
> auto_ptr<Foo> bar(new Foo);
> ...
> delete bar; // bar is implicitly converted to Foo*, so this is legal
But these programs are legal too.
int* p = new int;
delete p;
delete p;
int i;
delete &i;
char a[5];
delete [] a; // not sure for this one
{
auto_ptr<Foo> bar(new Foo);
bar.~auto_ptr<Foo> ();
}
The user has to know what's an auto_ptr in order to use it.
I don't think the argument is very powerfull.
--
Valentin Bonnard
mailto:bonnardv@pratique.fr
http://www.pratique.fr/~bonnardv (Informations sur le C++ en Francais)
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Ian Haggard <ian@shellus.com>
Date: 1997/01/08 Raw View
[Moderator's note: this is a repost. When this article was posted
previously, it somehow got mutilated: the five lines beginning with
"...", including the author's signature, were omitted. mha]
In article <01bbfab9$d2f13ee0$a10f9aa5@albert>,
"Anson Tsao" <ansont@hookup.net> wrote:
>
> One more auto_ptr question:
>
> Why doesn't auto_ptr implement
> template class<T> T* auto_ptr<T>::operator T*() ?
> Would be more readable than using auto_ptr::get();
>
> ---------------------------------------
> Anson Tsao
> TKK Inc.
> Software Development Group
Scott Meyers has some excellent sections dealing with auto_ptr and other
types of smart pointers in "More Effective C++". He also has a section
which talks about the problems that can be caused by user-defined
conversions. If you really want the long answer to this and lots of
other questions you may have about auto_ptr and other smart pointers,
I'd recommend reading his book. BTW, he also has some updated
information about auto_ptr on-line at http://www.aw.com/cp/mec++.html.
The short answer is that having and operator T*() would make possible
travesties such as this:
auto_ptr<Foo> bar(new Foo);
...
delete bar; // bar is implicitly converted to Foo*, so this is legal
--
Ian Haggard || ian@shellus.com (work) || IanHaggard@juno.com (home)
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Anson Tsao" <ansont@hookup.net>
Date: 1997/01/06 Raw View
One more auto_ptr question:
Why doesn't auto_ptr implement
template class<T> T* auto_ptr<T>::operator T*() ?
Would be more readable than using auto_ptr::get();
---------------------------------------
Anson Tsao
TKK Inc.
Software Development Group
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Ian Haggard <ian@shellus.com>
Date: 1997/01/07 Raw View
In article <01bbfab9$d2f13ee0$a10f9aa5@albert>,
"Anson Tsao" <ansont@hookup.net> wrote:
>
> One more auto_ptr question:
>
> Why doesn't auto_ptr implement
> template class<T> T* auto_ptr<T>::operator T*() ?
> Would be more readable than using auto_ptr::get();
>
> ---------------------------------------
> Anson Tsao
> TKK Inc.
> Software Development Group
Scott Meyers has some excellent sections dealing with auto_ptr and other
types of smart pointers in "More Effective C++". He also has a section
which talks about the problems that can be caused by user-defined
conversions. If you really want the long answer to this and lots of
other questions you may have about auto_ptr and other smart pointers,
I'd recommend reading his book. BTW, he also has some updated
information about auto_ptr on-line at http://www.aw.com/cp/mec++.html.
The short answer is that having and operator T*() would make possible
travesties such as this:
auto_ptr<Foo> bar(new Foo);
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: RBremer@Future-Gate.com (Ronny Bremer)
Date: 1996/10/16 Raw View
davidb@datalytics.com (David Bradley) wrote:
>RBremer@Future-Gate.com (Ronny Bremer) wrote:
>>Where is the problem with defining my own class auto_ptr_array ?
>None, and we use them here. It's just not something I run into a lot.
>Usually only OS related stuff where I have to have some kind of buffer
>for a OS function call.
As I do.
>For much of anything else I use a collection class such as vector that
>handles bounds checking and such to catch my stupid mistakes.
Agreed, they are better for "NON CHAR" arrays, but sometimes you have
to deal with system calls which do not support C++.
Regards,
Ronny
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: angelo@iii.org.tw (Supe3 User)
Date: 1996/10/09 Raw View
As title. Is there any consideration for not doing so?
Angelo Huang-Ming Hunag
Institute for Information Industry
angelo@iii.org.tw
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Matt Austern <austern@isolde.mti.sgi.com>
Date: 1996/10/09 Raw View
angelo@iii.org.tw (Supe3 User) writes:
> As title. Is there any consideration for not doing so?
The reason is that auto_ptr can't be used for an array pointer, only
for a pointer to a single object.
This limitation isn't because of a design flaw in auto_ptr; it has to
be that way. Remember that auto_ptr deletes the memory it controls.
The syntax for deleting a single object isn't the same as the syntax
for deleting an array; in the former case you use delete, and in the
latter case you use delete[]. Since auto_ptr uses delete, can't be
uses with a pointer to an array.
The container class vector might do everything that you need.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: davidb@datalytics.com (David Bradley)
Date: 1996/10/09 Raw View
It's an auto_PTR not an auto_ARRAY. If you're dealing with arrays
then you'd be better off using the appropriate container class that
has an array operator.
--------------------------------------------
David Bradley davidb@datalytics.com
Software Engineer
Datalytics, Inc. http://www.datalytics.com
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: bvisscher@spyder.net (Bruce Visscher)
Date: 1996/10/10 Raw View
On 9 Oct 1996 16:40:56 GMT, angelo@iii.org.tw (Supe3 User) wrote:
>As title. Is there any consideration for not doing so?
This would not be appropriate. Since the destructor deletes an owned
pointer via "delete" rather than "delete []" you should not initialize
it with a "new T[x]" expression.
OTOH, after using the auto_ptr class a few times, I have been
wondering why there's no bool operator!(). Has this been considered?
Bruce
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: RBremer@Future-Gate.com (Ronny Bremer)
Date: 1996/10/10 Raw View
angelo@iii.org.tw (Supe3 User) wrote:
>As title. Is there any consideration for not doing so?
What should operator[] do in your oppinion ?
Regards,
Ronny Bremer
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: RBremer@Future-Gate.com (Ronny Bremer)
Date: 1996/10/13 Raw View
davidb@datalytics.com (David Bradley) wrote:
>It's an auto_PTR not an auto_ARRAY. If you're dealing with arrays
>then you'd be better off using the appropriate container class that
>has an array operator.
OK. But what about the good old char[] ? Sometimes it is very nice to
have something like:
void foo ()
{
auto_ptr_arry<char> myChar = new char[128];
strcpy(myChar, "ABC");
// delete myChar automatically !
}
Where is the problem with defining my own class auto_ptr_array ?
Regards,
Ronny Bremer
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: davidb@datalytics.com (David Bradley)
Date: 1996/10/14 Raw View
RBremer@Future-Gate.com (Ronny Bremer) wrote:
>Where is the problem with defining my own class auto_ptr_array ?
None, and we use them here. It's just not something I run into a lot.
Usually only OS related stuff where I have to have some kind of buffer
for a OS function call.
For much of anything else I use a collection class such as vector that
handles bounds checking and such to catch my stupid mistakes.
--------------------------------------------
David Bradley davidb@datalytics.com
Software Engineer
Datalytics, Inc. http://www.datalytics.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]