Topic: Why no iterator::operator T*() in STL?


Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: 1995/08/02
Raw View
In article <1995Jul19.180847.17243@ptolemy-ethernet.arc.nasa.gov>,
Paul J. Lucas <pjl@kronos.arc.nasa.gov> wrote:
> Why shouldn't I be able to do:
>
>  list< int > x;
>  list< int >::iterator i;
>  int *p = i; // use list< int >::operator int*()
>
> i.e., convert the iterator to its underlying pointer?

 Use:

 int *p = &*i;

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,
        81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
        NSW 2037, AUSTRALIA     Phone: 61-2-566-2189





Author: herbs@interlog.com (Herb Sutter)
Date: 1995/08/03
Raw View
In article <3vo6iv$grf@metro.ucc.su.OZ.AU>,
   maxtal@Physics.usyd.edu.au (John Max Skaller) wrote:
>In article <1995Jul19.180847.17243@ptolemy-ethernet.arc.nasa.gov>,
>Paul J. Lucas <pjl@kronos.arc.nasa.gov> wrote:
>> Why shouldn't I be able to do:
>>
>>  list< int > x;
>>  list< int >::iterator i;
>>  int *p = i; // use list< int >::operator int*()

Do you mean list<int>::iterator::operator int*() here?

>>
>> i.e., convert the iterator to its underlying pointer?
>
> Use:
>
> int *p = &*i;

Right.  Also, Paul, for an entertaining treatment of why implicit conversions
(such as the operator int*() above) can become quite evil and foul-smelling in
practice even for their original programmer, see Scott Meyers' column in this
month's C/C++ User's Journal.

This is one of the rare examples where an otherwise neat and clean-cut
standards-conforming C++ compiler will go out of its way to silently and
mericlessly destroy your code given the slightest excuse, and will go about
doing so in the most devilishly difficult-to-debug way it can find to boot...
:-)


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Herb Sutter                 2228 Urwin, Ste 102         voice (416) 618-0184
Connected Object Solutions  Oakville ON Canada L6L 2T2    fax (905) 847-6019





Author: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1995/07/24
Raw View
In article <DC07tr.Er9@online.tmx.com.au> tony@online.tmx.com.au (Tony
Cook) writes:

|> Paul J. Lucas (pjl@kronos.arc.nasa.gov) wrote:
|> :  Why shouldn't I be able to do:

|> :   list< int > x;
|> :   list< int >::iterator i;
|> :   int *p = i; // use list< int >::operator int*()

|> :  i.e., convert the iterator to its underlying pointer?

|> What's wrong with:
|>    int *p = &*i; // use list< int >::operator *()

I'm not sure (I'm not completely up to date with the library issues in
the standard yet), but I suspect that this would (or should) be
illegal to.  I would hope that the implementation is free to have
list< int >::iterator::operator*() return a proxy.

Whether the actual list class in the standard is free to or not, I
believe that this freedom is necessary in the STL model.  That is: a
user defined class should not be considered to not conform to the STL
specifications if it does so.  For example, it should be possible to
write containers which actually store there data on disks and are STL
conformant.  Such containers need to have the iterator return a proxy
in order to be able to tell whether the user is writing or reading.
--
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                              -- Beratung in industrieller Datenverarbeitung







Author: tony@online.tmx.com.au (Tony Cook)
Date: 1995/07/20
Raw View
Paul J. Lucas (pjl@kronos.arc.nasa.gov) wrote:
:  Why shouldn't I be able to do:

:   list< int > x;
:   list< int >::iterator i;
:   int *p = i; // use list< int >::operator int*()

:  i.e., convert the iterator to its underlying pointer?

What's wrong with:
   int *p = &*i; // use list< int >::operator *()
--
        Tony Cook - tony@online.tmx.com.au
                    100237.3425@compuserve.com





Author: pjl@kronos.arc.nasa.gov (Paul J. Lucas)
Date: 1995/07/19
Raw View
 Why shouldn't I be able to do:

  list< int > x;
  list< int >::iterator i;
  int *p = i; // use list< int >::operator int*()

 i.e., convert the iterator to its underlying pointer?
--
 - Paul J. Lucas
   NASA Ames Research Center  Caelum Research Corporation
   Moffett Field, California  Mountain View, California





Author: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1995/07/20
Raw View
In article <1995Jul19.180847.17243@ptolemy-ethernet.arc.nasa.gov>
pjl@kronos.arc.nasa.gov (Paul J. Lucas) writes:

|>  Why shouldn't I be able to do:

|>   list< int > x;
|>   list< int >::iterator i;
|>   int *p = i; // use list< int >::operator int*()

|>  i.e., convert the iterator to its underlying pointer?

Because the iterator may not have an underlying pointer.  (On the
other hand, it must have an overloaded operator*, so that it can be
used like a pointer.)
--
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                              -- Beratung in industrieller Datenverarbeitung







Author: bflorman@indy.net (Bruce A. Florman)
Date: 1995/07/20
Raw View
Paul J. Lucas (pjl@kronos.arc.nasa.gov) wrote:
:  Why shouldn't I be able to do:

:   list< int > x;
:   list< int >::iterator i;
:   int *p = i; // use list< int >::operator int*()

Well how about: int *p = &*i;

Two extra keystrokes, big deal.

--Bruce