Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-08-22 16:06:45


"Alex Chovanec" <achovane_at_[hidden]> writes:

> 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

I don't believe that's detectable without generating an error. Don't
forget that iterator_traits can be specialized; there's no reason the
iterator has to contain members. Quick test: what is
is_iterator<int*>::value?

> 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.

There ain't none AFAICT.

> 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?

Yes, if it's implementable. But I don't think it is.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk