Re: nulliter
On Oct 28, 10:20 pm, Don Stock <d...@yahoo.com> wrote:
It would be most convenient if iterators had a standard value
"nulliter" that means "references nothing". I see that this topic has
been resurfacing for years, and the usual answers are "iterators
aren't pointers" and "use end()".
Using end() works if (a) the container object exists, and (b) the
value of end() never changes. Unfortunately, both those cases can be
excepted. And what about functions that take an iterator and don't
need to know which container it's for?
As for iterators not being pointers, that misses the point. The
attributes of a variable often include being able to say that it
doesn't apply to anything. It's true that a separate bool could be
used for that, but that's less elegant than embedding that meaning
within the value set of the variable. Saying that you can't have
nulliter for iterators is like saying you can't have -1 for an array's
index var.
You can have it:
template<class Container>
class nulliter
{
private:
static Container empty_container;
public:
typedef typename Container::iterator iterator;
static iterator const value;
};
template<class Container>
Container nulliter<Container>::empty_container = Container();
template<class Container>
typename nulliter<Container>::iterator const
nulliter<Container>::value =
nulliter<Container>::empty_container.end();
Usage:
#include <list>
int main()
{
std::list<int>::iterator iter = nulliter<std::list<int>
::value;
}
--
Max
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]