Topic: Proposed extension to iterators (Was: Proposed extension to STL containers)
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/08/18 Raw View
Vlad Harchev <vladhar@imimail.ssau.ru> writes:
> Possible extension to standart: A lot of algorithms that store iterators
> in some containers or objects require the special meaning for some
> iterator.
> 2) Some compilers doesn't allow overlaoding on enums,
Broken compilers have nothing to do with this problem.
> 3) IMHO, this simple extension has particular importance and should be
> considered by STL writers.
This extension would break all existing iterators except pointers.
It would complicate iterators.
It would complicate client code which would have to consider
one more state for iterators (beside the existing ones):
- invalid
- one-past-the-end
- dereferencable
If you need a special value, use one-past-the-end (for example
as the return of find). If you need yet another special value,
code it yourself.
Your proposal doesn't help people who need more special values,
nor people would need special values with other types. The
general solution is a template:
template <typename T>
class Optionnal
{
public:
class not_present : public logic_error
{
public:
not_present ();
};
Optionnal ();
// post: present () == false
Optionnal (const T& v);
// post: present () == true, get () same as v
bool present () const throw ();
T get () const;
// pre: present () == true
// throws: not_present
void set (const T& v);
// post: present () == true, get () same as v
void reset ();
// post: present () == false
private:
bool defined;
T value;
};
Type requirement: T is Assignable and DefaultConstructible
Note that in particular, iterators meat these requirements.
I am strongly opposed to your idea.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
[ 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 ]