Re: Deprecate the use of plain pointers as standard container iterators
kuyper@wizard.net () wrote (abridged):
Yes, but in order to show that there really is a problem that calls for
a change to the standard, it's helpful to provide an example where the
problem that's being demonstrated is not easily fixed by methods that
don't involve changing the standard.
For me the problem is with code like:
#include <vector>
void func( std::vector<int>::iterator ) {}
void func( int * ) {}
On some platforms this defines two functions, and on others it is an error
because it defines the same function twice.
The easiest attempt at a workaround is to omit the iterator overload, and
change the point of call:
std::vector<int>::iterator i;
int *p;
// ... initialise i and p...
func( p ); // Works always.
func( &*i ); // Works if i is not v.end().
func( &*p ); // Works if p is not NULL.
func( i ); // Works on some platforms.
The "&*i" adds clutter, and puts a burden onto users of func() to remember
and use it, and if they forget the compiler may not warn them (depending
on the platform), and it doesn't work if i is singular. It's not very
good, really.
Nicola Musatti's suggestion would be a better solution.
-- Dave Harris, Nottingham, UK.
---
[ 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://www.comeaucomputing.com/csc/faq.html ]