Topic: compound operator &* for iterators


Author: "Nicolas Nombela" <istz4356@tsai.es>
Date: 2000/02/15
Raw View
I'm often in the need of having to use the curious expression &*p for taking
the address of an element pointed by the iterator p. I find it far from
elegant. Being iterators a generalization of a pointer for movement through
containers, wouldn't be more appropriate having defined operator pointer()
const in the iterators definition? Is it any good reason that should not be
done?

Nicolas  (istz4356@tsai.es)



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Hyman Rosen <hymie@prolifics.com>
Date: 2000/02/16
Raw View
"Nicolas Nombela" <istz4356@tsai.es> writes:
> I'm often in the need of having to use the curious expression &*p for taking
> the address of an element pointed by the iterator p. I find it far from
> elegant. Being iterators a generalization of a pointer for movement through
> containers, wouldn't be more appropriate having defined operator pointer()
> const in the iterators definition? Is it any good reason that should not be
> done?

But pointers can act as iterators, and they don't have any named methods.
If you want a conversion from iterators to pointers, just write one -

#include <iterator>

template <typename Iterator>
inline typename std::iterator_traits<Iterator>::value_type *
pointer(Iterator p)
{
 return &*p;
}

and now given an iterator p, pointer(p) will be a pointer to the object that
the iterator references.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]