473,320 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Smart Pointers : auto_ptr and array

Hi,

Let's say I have a vector of auto_ptr defined like this :

vector< auto_ptr<T v;

is it allowed ?
Is there any side effects ?

If it's not a good idea, how can I fix this ?
May 16 '07 #1
10 3789
* mosfet:
>
vector< auto_ptr<T v;

is it allowed ?
No, because auto_ptr is not copyable, and a good compiler will complain.

Is there any side effects ?
Yes, if it compiles.

If it's not a good idea, how can I fix this ?
Use e.g. boost::shared_ptr.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 16 '07 #2
On 5/16/2007 11:41 AM, mosfet wrote:
Hi,

Let's say I have a vector of auto_ptr defined like this :

vector< auto_ptr<T v;

is it allowed ?
Is there any side effects ?
No. Not allowed. Might not compile. Don't !
If it's not a good idea, how can I fix this ?
std::vector<boost::shared_ptr<T v;
S.
--
Stefan Naewe
stefan dot naewe at atlas-elektronik dot com
May 16 '07 #3
On 16 May, 10:41, mosfet <john....@anonymous.orgwrote:
Hi,

Let's say I have a vector of auto_ptr defined like this :

vector< auto_ptr<T v;

is it allowed ?
NO. read http://www.ddj.com/dept/cpp/184403719
Is there any side effects ?
If it's not a good idea, how can I fix this ?
use boost/TR1 shared_ptr instead.

May 16 '07 #4
On May 16, 12:00 pm, "Alf P. Steinbach" <a...@start.nowrote:
* mosfet:
vector< auto_ptr<T v;
is it allowed ?
No, because auto_ptr is not copyable, and a good compiler will
complain.
Is there any side effects ?
Yes, if it compiles.
I'm no longer very sure about this---the specification of
auto_ptr seems to change each time I look---but wasn't the
motivation behind its use of auto_ptr_ref, or whatever,
precisely so that trying to instantiate std::vector on one
couldn't compile?

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 16 '07 #5
* James Kanze:
On May 16, 12:00 pm, "Alf P. Steinbach" <a...@start.nowrote:
>* mosfet:
>>vector< auto_ptr<T v;
>>is it allowed ?
>No, because auto_ptr is not copyable, and a good compiler will
complain.
>>Is there any side effects ?
>Yes, if it compiles.

I'm no longer very sure about this---the specification of
auto_ptr seems to change each time I look---but wasn't the
motivation behind its use of auto_ptr_ref, or whatever,
precisely so that trying to instantiate std::vector on one
couldn't compile?
You're asking more than I currently know. I probably did know that at
one time, and I think Howard Hinnant knows. The auto_ptr_ref trick
enables construction from a temporary (into a copy constructor with
non-const formal arg), but what its main motivation was...

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 19 '07 #6
On May 19, 5:05 am, "Alf P. Steinbach" <a...@start.nowrote:
* James Kanze:
On May 16, 12:00 pm, "Alf P. Steinbach" <a...@start.nowrote:
* mosfet:
>vector< auto_ptr<T v;
>is it allowed ?
No, because auto_ptr is not copyable, and a good compiler will
complain.
>Is there any side effects ?
Yes, if it compiles.
I'm no longer very sure about this---the specification of
auto_ptr seems to change each time I look---but wasn't the
motivation behind its use of auto_ptr_ref, or whatever,
precisely so that trying to instantiate std::vector on one
couldn't compile?
You're asking more than I currently know. I probably did know that at
one time, and I think Howard Hinnant knows. The auto_ptr_ref trick
enables construction from a temporary (into a copy constructor with
non-const formal arg), but what its main motivation was...
It may even date before his time:-).

I know that auto_ptr went through at least three versions before
the first CD, and IIRC, auto_ptr_ref was originally introduced
because the British national body threatened to vote no on the
standard unless something was done to prevent instantiation of a
container on an auto_ptr. Since then: the version in the 2003
version of the standard is significantly different than that in
the 1998, so it continued to move after I stopped looking.

About all I know is that today, I can generally use it for the
purposes I want it for, with just about any compiler. Which is
considerable progress.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 19 '07 #7
In article <11**********************@w5g2000hsg.googlegroups. com>,
James Kanze <ja*********@gmail.comwrote:
On May 19, 5:05 am, "Alf P. Steinbach" <a...@start.nowrote:
* James Kanze:
On May 16, 12:00 pm, "Alf P. Steinbach" <a...@start.nowrote:
>* mosfet:
>>vector< auto_ptr<T v;
>>is it allowed ?
>No, because auto_ptr is not copyable, and a good compiler will
>complain.
>>Is there any side effects ?
>Yes, if it compiles.
I'm no longer very sure about this---the specification of
auto_ptr seems to change each time I look---but wasn't the
motivation behind its use of auto_ptr_ref, or whatever,
precisely so that trying to instantiate std::vector on one
couldn't compile?
You're asking more than I currently know. I probably did know that at
one time, and I think Howard Hinnant knows. The auto_ptr_ref trick
enables construction from a temporary (into a copy constructor with
non-const formal arg), but what its main motivation was...

It may even date before his time:-).
<nodIt does.
I know that auto_ptr went through at least three versions before
the first CD, and IIRC, auto_ptr_ref was originally introduced
because the British national body threatened to vote no on the
standard unless something was done to prevent instantiation of a
container on an auto_ptr. Since then: the version in the 2003
version of the standard is significantly different than that in
the 1998, so it continued to move after I stopped looking.

About all I know is that today, I can generally use it for the
purposes I want it for, with just about any compiler. Which is
considerable progress.
See the latest working draft:

http://www.open-std.org/jtc1/sc22/wg...2007/n2284.pdf

for the latest change in the continually evolving auto_ptr:

It's deprecated. ;-)

Currently the best write up I have on auto_ptr, why it is deprecated,
and its replacement unique_ptr (which *will* work in containers) is:

http://www.open-std.org/jtc1/sc22/wg...56.html#20.4.5
%20-%20Class%20template%20auto_ptr

-Howard
May 25 '07 #8
On May 25, 10:04 pm, Howard Hinnant <howard.hinn...@gmail.comwrote:
In article <1179563347.135800.218...@w5g2000hsg.googlegroups. com>,
James Kanze <james.ka...@gmail.comwrote:
About all I know is that today, I can generally use it for the
purposes I want it for, with just about any compiler. Which is
considerable progress.
See the latest working draft:
http://www.open-std.org/jtc1/sc22/wg...2007/n2284.pdf
for the latest change in the continually evolving auto_ptr:

It's deprecated. ;-)
Currently the best write up I have on auto_ptr, why it is deprecated,
and its replacement unique_ptr (which *will* work in containers) is:
http://www.open-std.org/jtc1/sc22/wg.../n1856.html#20...
%20-%20Class%20template%20auto_ptr
So you invent a new name for something which serves more or less
the same purpose.

Just curious, but why the new name? Are there any legal uses of
auto_ptr that won't work exactly the same with unique_ptr? And
if not, why not call it auto_ptr, assuming that that doesn't
break any working code? (I use auto_ptr a lot in multithreaded
programs, even when I'm also using the Boehm collector.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 25 '07 #9
In article <11*********************@g4g2000hsf.googlegroups.c om>,
James Kanze <ja*********@gmail.comwrote:
On May 25, 10:04 pm, Howard Hinnant <howard.hinn...@gmail.comwrote:
In article <1179563347.135800.218...@w5g2000hsg.googlegroups. com>,
James Kanze <james.ka...@gmail.comwrote:
About all I know is that today, I can generally use it for the
purposes I want it for, with just about any compiler. Which is
considerable progress.
See the latest working draft:
http://www.open-std.org/jtc1/sc22/wg...2007/n2284.pdf
for the latest change in the continually evolving auto_ptr:

It's deprecated. ;-)
Currently the best write up I have on auto_ptr, why it is deprecated,
and its replacement unique_ptr (which *will* work in containers) is:
http://www.open-std.org/jtc1/sc22/wg.../n1856.html#20...
%20-%20Class%20template%20auto_ptr

So you invent a new name for something which serves more or less
the same purpose.
The new name exists because unique_ptr is not an exact superset of the
auto_ptr syntax. If it were, , we could just fix auto_ptr instead of
deprecating it and introducing a new name.
Just curious, but why the new name?
Read the link. At the top of the read is the reason for deprecating
auto_ptr and substituting in unique_ptr as the replacement. If we could
simply reengineer auto_ptr without breaking existing code we would. But
we can't.
Are there any legal uses of
auto_ptr that won't work exactly the same with unique_ptr? And
if not, why not call it auto_ptr, assuming that that doesn't
break any working code?
From the paper:
However because unique_ptr does not move from lvalues with copy syntax, it is
not a 100% source-compatible drop in replacement for auto_ptr. If it were, we
could just fix auto_ptr instead of deprecating it and introducing a new class
template.

(I use auto_ptr a lot in multithreaded
programs, even when I'm also using the Boehm collector.)
And that is the reason auto_ptr has been deprecated, not removed, from
the standard. We understand that it is heavily used and must remain in
the standard for now. Deprecation doesn't mean non-standard. It merely
means that you are put on notice that it might be removed from a future
standard (say in 2019).

Within that decade ahead of us, I hope that most uses of auto_ptr will
be able to migrate to unique_ptr. But that migration will, in some
cases, require more than just renaming auto_ptr to unique_ptr. The link
points out the breakage under this migration:
However copy semantics is disabled with unique_ptr:

auto_ptr<intap1(new int);
auto_ptr<intap2 = ap1; // ok, but unsafe implicit move

unique_ptr<intup1(new int);
unique_ptr<intup2 = up1; // compile time error: illegal access to
// private copy constructor

If you really want to transfer ownership from the lvalue unique_ptr, you move
from it just as you would any other type:

unique_ptr<intup1(new int);
unique_ptr<intup2 = move(up1); // ok, explicit move
In the interim auto_ptr and unique_ptr will coexist in the standard.
unique_ptr will support a superset of the functionality of auto_ptr, but
with slightly different syntax when "copying" (actually moving) from
lvalues.

-Howard
May 25 '07 #10
mosfet wrote:
If it's not a good idea, how can I fix this ?
Use a garbage collected language.

--
Dr Jon D Harrop, Flying Frog Consultancy
OCaml for Scientists
http://www.ffconsultancy.com/product...ntists/?usenet
May 30 '07 #11

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

Similar topics

1
by: Evan | last post by:
Chandra Shekhar Kumar <chandra.kumar@oracle.com> wrote in message news:<3EF64860.3FCC6E7F@oracle.com>... > donot use dude pointers ...use smart pointer instead There's a problem with this...
14
by: David B. Held | last post by:
I wanted to post this proposal on c.l.c++.m, but my news server apparently does not support that group any more. I propose a new class of exception safety known as the "smart guarantee". ...
9
by: Mohammad | last post by:
Is there a difference between smart pointers and automatic pointers or they refer to the same idea. -Thanks
9
by: christopher diggins | last post by:
I would like to survey how widespread the usage of smart pointers in C++ code is today. Any anecdotal experience about the frequency of usage of smart pointer for dynamic allocation in your own...
27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
4
by: Matthias Kaeppler | last post by:
Hi, I'm having a hard time figuring out how I can initialize a smart pointer based on a certain condition: if something then ptr = 0; // init with NULL else ptr = new XYZ; // init with a...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
8
by: Simon L | last post by:
Pointer mismanagement is a big cause of bugs in software that I work on. I decided to invest a little time into having a look at auto_ptr and other smart pointers but I don't see how they'll save...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.