At the end of Item 52 in "C++ Coding Standards" (about copying and
destroying objects consistently) it says:
"(Note that using a reference or auto_ptr member is almost always
wrong.)"
This seems quite a strong statement and unfortunately the book does not
elaborate. (A lot of effort seems to have gone into squeezing every
item in the book so that it ends at the bottom of a page, which I think
makes some of them overly concise.)
Whilst I can see that there are plenty of circumstances when it is
wrong, are there not quite a few circumstances where it's OK -- and
indeed useful -- to use a reference or an auto_ptr member? Eg wouldn't
a reference to a singleton usually be OK; and isn't auto_ptr often used
in the pimpl idiom? Or am I missing some other pitfall?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
palaso (2)
|
8/3/2005 10:36:03 PM |
|
Matt Young wrote:
> At the end of Item 52 in "C++ Coding Standards" (about copying and
> destroying objects consistently) it says:
>
> "(Note that using a reference or auto_ptr member is almost always
> wrong.)"
>
> This seems quite a strong statement and unfortunately the book does not
> elaborate. (A lot of effort seems to have gone into squeezing every
> item in the book so that it ends at the bottom of a page, which I think
> makes some of them overly concise.)
>
> Whilst I can see that there are plenty of circumstances when it is
> wrong, are there not quite a few circumstances where it's OK -- and
> indeed useful -- to use a reference or an auto_ptr member?
I just reread the paragraph and don't understand it either. I believe that
if you take into account that they talk about copying and assignment
earlier, this is meant for value-types[1]: the peculiar semantics of
copying and assigning references and auto_ptr doesn't fit a value-type
objects, so they shouldn't contain them.
> Eg wouldn't a reference to a singleton usually be OK;
Usually, yes. However, there is effectively nothing that makes sure the
object outlives the reference to it, which makes this a bit dangerous.
> and isn't auto_ptr often used in the pimpl idiom?
PIMPL is so simple to get right that an auto_ptr doesn't help that much.
Also, remember that auto_ptr needs a complete type to destroy it, so not
even the dtor can be left at the compiler-generated default or inline in
the declaration:
class foo: noncopyable
{
class impl;
auto_ptr<impl> m_pimpl;
public:
foo();
~foo(){}
};
Will lead to undefined behaviour - you need to move the dtor to after the
definition of foo::impl.
Uli
[1] Copyable value-types as opposed to object-types, which usually also
can't be copied. There were better names for those two different types, but
I forgot...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Ulrich
|
8/4/2005 10:40:01 AM
|
|
Matt Young wrote:
> At the end of Item 52 in "C++ Coding Standards" (about copying
> and destroying objects consistently) it says:
> "(Note that using a reference or auto_ptr member is almost always
> wrong.)"
> This seems quite a strong statement and unfortunately the book
> does not elaborate. (A lot of effort seems to have gone into
> squeezing every item in the book so that it ends at the bottom
> of a page, which I think makes some of them overly concise.)
Elliptical comments in parentheses thrown in at the end of a
page are almost always wrong:-).
> Whilst I can see that there are plenty of circumstances when
> it is wrong, are there not quite a few circumstances where
> it's OK -- and indeed useful -- to use a reference or an
> auto_ptr member? Eg wouldn't a reference to a singleton
> usually be OK; and isn't auto_ptr often used in the pimpl
> idiom? Or am I missing some other pitfall?
In general, if the class must support assignment, references and
auto_ptr are wrong. Otherwise, I see no problem with them. In
value classes, or things like STL predicates, I avoid them; in
entity classes, however, I use them freely.
--
James Kanze GABI Software
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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
kanze
|
8/4/2005 10:48:42 AM
|
|
Matt Young wrote:
>
> "(Note that using a reference or auto_ptr member is almost always
> wrong.)"
[...]
> Whilst I can see that there are plenty of circumstances when it is
> wrong, are there not quite a few circumstances where it's OK -- and
> indeed useful -- to use a reference or an auto_ptr member? Eg wouldn't
> a reference to a singleton usually be OK;
Perhaps, but so would a pointer to the singleton. And using a pointer
would allow the defalt assignment operator to work.
> and isn't auto_ptr often used
> in the pimpl idiom?
A const std::auto_ptr is occasionally used, but there are usually
better choices. (The problem with a non-const std::auto_ptr is that it
gives the class's copy constructor move semantics which is almost
always wrong.) The other problem with using std::auto_ptr is that
there is no guarantee that it can be used with an incomplete type (the
whole point of the pimpl idiom). By contrast, std::tr1::shared_ptr (or
boost::shared_ptr if you prefer) is specifically designed to work with
a incomplete types.
--
Richard Smith
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
richard
|
8/4/2005 12:29:42 PM
|
|
|
3 Replies
193 Views
(page loaded in 0.093 seconds)
|