473,660 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Which is preferable std::auto_ptr or boost's smart pointer?

How do you think?
and why?
Jul 22 '05 #1
9 2245
BekTek wrote:
How do you think?
and why?


Boost has several different smart pointer types, each tailored for a
specific need, which allows for more flexibility. I would go with boost.
I have even heard that boost smart pointers are subject to enter the C++
standard library on the next revision.

- Matthias
Jul 22 '05 #2
In article <co************ *@news.t-online.com>,
Matthias Kappler <no****@digital raid.com> wrote:
BekTek wrote:
How do you think?
and why?


Boost has several different smart pointer types, each tailored for a
specific need, which allows for more flexibility. I would go with boost.
I have even heard that boost smart pointers are subject to enter the C++
standard library on the next revision.

- Matthias


Proposals based on boost::shared_p tr and boost::weak_ptr have been
accepted into the first library technical report (TR1). This does not
mean that they are standard, only that they are being seriously
considered for standardization . Other boost smart pointers (scoped_ptr,
shared_array, intrusive_ptr, etc.) have not been proposed.

As to the OP's original question, it is not answerable without some
context on what the smart pointer is supposed to do. I would not advise
using shared_ptr when auto_ptr is a better fit, nor vice-versa.

-Howard
Jul 22 '05 #3

"BekTek" <be****@gmail.c om> wrote in message news:5Ehqd.3000 $xk5.755@trnddc 09...
How do you think?
and why?


As others have mentioned, boost::shared_p tr and std::auto_ptr are useful in
different types of situations. If you decide that auto_ptr is more appropriate,
however, you might also consider this implementation of move_ptr (based in large
part on Howard's work):

http://home.comcast.net/~jturkanis/move_ptr/

(It is used as an implementation detail in the recently reviewed Boost Smart
Containers library, by Thorsten Ottosen, and may therefore soon become an
undocumented part of Boost.)

The library documentation gives a detailed analysis of the differences between
move_ptr and auto_ptr, as well as some useful references on auto_ptr.

Jonathan

Jul 22 '05 #4
"BekTek" <be****@gmail.c om> wrote in message
news:5Ehqd.3000 $xk5.755@trnddc 09...
How do you think?
and why?


They both have their uses, so I wouldn't call one "better". However,
std::auto_ptr does have a characteristic which is hard to like: for
auto_ptr's x and y

y = x;

changes both x and y. This can certainly cause confusion in use. A similar
propery of auto_ptr's copy constructor makes in unusable in standard
containers such as std::vector. Also note that a class which contains an
auto_ptr as a member will have the same dubioius properties.

boost::shared_p tr has none of these issues.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #5

"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote in message
news:qj******** **********@twis ter.nyroc.rr.co m...
"BekTek" <be****@gmail.c om> wrote in message
news:5Ehqd.3000 $xk5.755@trnddc 09...
How do you think?
and why?


They both have their uses, so I wouldn't call one "better". However,
std::auto_ptr does have a characteristic which is hard to like: for
auto_ptr's x and y

y = x;

changes both x and y. This can certainly cause confusion in use.


This one of the main attractions of move_ptr: the above won't compile. If you
really want to transfer ownership from x to y, you have to write

y = move(x);

Jonathan
Jul 22 '05 #6
In article <30************ *@uni-berlin.de>,
"Jonathan Turkanis" <te******@kanga roologic.com> wrote:
"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote in message
news:qj******** **********@twis ter.nyroc.rr.co m...
"BekTek" <be****@gmail.c om> wrote in message
news:5Ehqd.3000 $xk5.755@trnddc 09...
How do you think?
and why?


They both have their uses, so I wouldn't call one "better". However,
std::auto_ptr does have a characteristic which is hard to like: for
auto_ptr's x and y

y = x;

changes both x and y. This can certainly cause confusion in use.


This one of the main attractions of move_ptr: the above won't compile. If you
really want to transfer ownership from x to y, you have to write

y = move(x);


And kudos to Jonathan for implementing and making this available in
C++03 (as much as possible). I'm very much looking forward to the day
when this animal can be put into std::containers , and used with
std::algorithms . :-)

-Howard
Jul 22 '05 #7
Jonathan Turkanis wrote:
"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote:
for auto_ptr's x and y

y = x;

changes both x and y. This can certainly cause confusion in use.
This one of the main attractions of move_ptr: the above won't

compile. If you really want to transfer ownership from x to y, you have to write

y = move(x);


Apparently I have become seriously stupid but I don't see it:

| std::auto_ptr<i nt> x(new int(1));
| std::auto_ptr<i nt> y;
| y = x;

You seem to assert that the assignment in the above statement does not
compile. I can see that it may have unexpected behavior but not that it
does not compile. Can you please shed some light on this? Maybe you
were
refering to the situation where a function is returning an 'auto_ptr':

| std::auto_ptr<i nt> foo() { return std::auto_ptr<i nt>(new int(1)); }
| int main() {
| std::auto_ptr<i nt> x = foo(); // OK: there is a special ctor (*)
| x = foo(); // error: need non-const reference
| }

(*) At least, this is supposed to work. I seem to remember a discussion
which somehow deduced that this actually does not work. Is this
what you are refering to?
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 22 '05 #8
In article <11************ *********@z14g2 000cwz.googlegr oups.com>,
"Dietmar Kuehl" <di***********@ yahoo.com> wrote:
Jonathan Turkanis wrote:
"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote:
for auto_ptr's x and y

y = x;

changes both x and y. This can certainly cause confusion in use.


This one of the main attractions of move_ptr: the above won't

compile. If you
really want to transfer ownership from x to y, you have to write

y = move(x);


Apparently I have become seriously stupid but I don't see it:

| std::auto_ptr<i nt> x(new int(1));
| std::auto_ptr<i nt> y;
| y = x;

You seem to assert that the assignment in the above statement does not
compile. I can see that it may have unexpected behavior but not that it
does not compile. Can you please shed some light on this? Maybe you
were
refering to the situation where a function is returning an 'auto_ptr':

| std::auto_ptr<i nt> foo() { return std::auto_ptr<i nt>(new int(1)); }
| int main() {
| std::auto_ptr<i nt> x = foo(); // OK: there is a special ctor (*)
| x = foo(); // error: need non-const reference
| }


Hi Dietmar,

What Jonathan meant was that if the type of the smart pointer is
move_ptr, instead of auto_ptr, then the assignment won't compile:

y = x; // compile time error

This is a safety feature of move_ptr, especially in generic code where
moving from lvalues with copy syntax can render the generic algorithm
incorrect.

-Howard
Jul 22 '05 #9

"Howard Hinnant" <hi*****@metrow erks.com> wrote:
"Dietmar Kuehl" <di***********@ yahoo.com> wrote: Hi Dietmar,

What Jonathan meant was that if the type of the smart pointer is
move_ptr, instead of auto_ptr, then the assignment won't compile:

y = x; // compile time error

This is a safety feature of move_ptr, especially in generic code where
moving from lvalues with copy syntax can render the generic algorithm
incorrect.
Yes, that's what I was trying to say. Sorry I wan't clear.
-Howard


Jonathan
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
17128
by: SerGioGio | last post by:
Hello ! When a ref. counted smart pointer (template) class is available (like boost::shared_ptr), is there any (semantic) reason to still use std::auto_ptr rather than this smart pointer ? Or should one use shared_ptr for every cases ? Thanks in advance for your remarks ! SerGioGio
10
2534
by: ma740988 | last post by:
Part of my confusion here is certainly my ignorance of templates and std::auto_ptr. Two topics, I've perused but need to really _delve_ into. In any event, consider the 'test' source. # include <iostream> # include <memory> class CallbackBase { public: virtual void operator()() const { };
20
3727
by: Bronek Kozicki | last post by:
Hi Please try this code. I think that it's perfectly legal. However when compiled under MSVC71 stack overflow happens in first line of main, thus second line is never executed. B.
1
2846
by: Guido Forthofer | last post by:
Hello, I convert my VC6 C++ project after VC7 (7.1) and have now an compiler error d:\Programme\Microsoft Visual Studio .NET 2003\Vc7\include\vector(810): error C2558: class 'std::auto_ptr<_Ty>' : no copy constructor available or copy constructor is declared 'explicit' with
8
2645
by: Marchello | last post by:
Hi all. I have a class with virtual functions. This class lives in DLL. In main program I use this class by obtaining his pointer (by function that's exports from dll). class CfromDLL { ... virtual int Foo(); ...
10
2617
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in section 4.2. The copy constructor is defined as: auto_ptr (auto_ptr& rhs) throw() : ap (rhs. release()) { }
3
1601
by: Mears | last post by:
I'm pretty sure the answer is no, but thought I'd check. I'm trying to protect a pointer to a C-based struct that needs to be deallocated through an API call. I was thinking of simply creating a wrapper class for the struct that calls the API function in its destructor, but I'm hoping there is a cleaner way of doing this.
7
2004
by: j.l.olsson | last post by:
Hello, I am using std::auto_ptr to try to keep ownership of resources straight. However, I became aware that exception objects must provide a copy constructor, which made me uncertain of the soundness of throwing std::auto_ptrs... It was explained to me that a possible problem case is: try {
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8539
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.