Topic: Q: auto_ptr


Author: jlm@two-oo-one.fr (Jean-Louis Moser)
Date: 1995/10/11
Raw View

I got the implementation of auto_ptr in the draft standard

- Is there a reason why the method operator=(T*) isn't
  implemented.
- Why operator= doesn't respect the classical sementic of
  this operator, returning T* to enable: p1 = p2 = p3;
- With this implementation it's not possible to build auto_ptr
  on const T*, because constructor takes a T* as argument.

jlm

ps: Could somebody give me pointers to good publications
    on "all about reference counting and smart pointers"
--
+----------------------------+------------------------------------+
|   Jean-Louis Moser         |    2001 SA                         |
|   tel: 33 (1)46.66.54.54   |    2, rue de la renaissance        |
|   Email: jlm@two-oo-one.fr |    F-92184 Antony Cedex            |
+----------------------------+------------------------------------+

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1995/10/13
Raw View
Jean-Louis Moser (jlm@two-oo-one.fr) wrote:


|> I got the implementation of auto_ptr in the draft standard

|> - Is there a reason why the method operator=(T*) isn't
|>   implemented.

Not that I know of.

|> - Why operator= doesn't respect the classical sementic of
|>   this operator, returning T* to enable: p1 = p2 = p3;

Because returning T* wouldn't work anymore than the classical solution
(returning auto_ptr< T >&).

Returning T* in the above example would result in both p1 and p2 owning
the pointer.

Returning auto_ptr< T >& would cause the above to have strange
semantics.  Since the operator= also modifies the right hand side,  this
would be the equivalent of:

    p1 = p3 ;
    p2 = 0 ;

|> - With this implementation it's not possible to build auto_ptr
|>   on const T*, because constructor takes a T* as argument.

Sure it is, for any given T.  Try it: auto_ptr< int >, T == int;
auto_ptr< const int >, T == const int.

What you cannot do is the following:

    auto_ptr< int >         p( new int ) ;
    auto_ptr< const int >   pc( p ) ;

Unlike real pointers, there is no automatic conversion from pointer to
int to pointer to const int.
--
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle--
                             --Beratung in industrieller Datenverarbeitung


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]