Boost logo

Boost :

From: Alex Chovanec (achovane_at_[hidden])
Date: 2004-08-22 12:06:41

Is there a utility in Boost which determines at compile time whether or not
a given type meets the most basic requirements for an iterator, without
generating an error if the test fails? (This last part is crucial, so
concept checks are insufficient.)

I recently needed something like this, and I couldn't seem to find it in any
of the libraries, so I went ahead and wrote a template metafunction called
'is_iterator' which gets the job done. It uses the same SFINAE techniques as
the enable_if library.

The declaration looks like this:

    template <typename T>
    struct is_iterator;

The 'is_iterator' struct template is a model of the "integral constant
expression" concept. Thus, 'is_iterator<T>::value' evaluates to true
if-and-only-if type T defines iterator traits as well as overloaded
operators with the following signatures:

    boost::iterator_reference<T>::type operator*(void) const;
    boost::iterator_pointer<T>::type operator->(void) const;
    T & operator++(void);
    T operator++(int);

These seem to be the minimal requirements (i.e. Readable and Incrementable)
according to the New Iterator Concepts. Ideally, T would also have to be a
Copy Constructible type, but I don't know of a reliable way to detect this.

Originally, i needed the ability to determine at compile time whether or not
a given type meets the requirements for a Readable Lvalue iterator. The
'is_readable_iterator' and 'is_lvalue_iterator' template metafunctions work
fine for this purpose, except that they both generate an error when passed a
template argument that is not an iterator at all.

Assuming I'm correct in stating that a utility like 'is_iterator' does not
already exist in Boost, is there any interest in such a utility?



Boost list run by bdawes at, gregod at, cpdaniel at, john at