Topic: Iterator Traits & some doubt
Author: Pete Becker <petebecker@acm.org>
Date: 1999/09/02 Raw View
Simone BORDET wrote:
>
> Hi,
>
> can someone explain me what are the iterator_traits for ?
> Look for example at the end of page 554 of C++ PL third edition by
> Stroustrup,
> why not use In::iterator_category() instead of
> iterator_traits<In>::iterator_category() ?
> It seems to me that iterator_traits redefine typedefs already existing in
> iterator...
It's because pointers can be used as iterators. Pointers, of course,
don't have members, so you have to use a separate class to hold
information about them.
--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.com
---
[ 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 ]
Author: "Darin Adler" <darin@bentspoon.com>
Date: 1999/09/03 Raw View
Simone BORDET <simon@most.it> wrote:
> can someone explain me what are the iterator_traits for ?
> Look for example at the end of page 554 of C++ PL third edition by
> Stroustrup,
> why not use In::iterator_category() instead of
> iterator_traits<In>::iterator_category() ?
> It seems to me that iterator_traits redefine typedefs already existing in
> iterator...
For some kinds of iterators, you can use In::iterator_category. But there
are other kinds of iterators where you can't. For example, pointers can be
used as iterators when you want to use standard algorithms on arrays. In
this case, the iterator does not have any member, and In::iterator_category
won't work.
-- Darin
---
[ 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 ]
Author: "Simone BORDET" <simon@most.it>
Date: 1999/09/02 Raw View
Hi,
can someone explain me what are the iterator_traits for ?
Look for example at the end of page 554 of C++ PL third edition by
Stroustrup,
why not use In::iterator_category() instead of
iterator_traits<In>::iterator_category() ?
It seems to me that iterator_traits redefine typedefs already existing in
iterator...
TIA
Simon
For those that don't have the book:
template<class T>
void F(T t) { // function that I want to use
return F_helper(t, iterator_traits<T>::iterator_category());
}
// the function can be optimized if I know the type of the typename T
template<class T1>
void F_helper(T1 t, tag1 dummy) {
// Here t is of the type defined by tag1, use specific functions of T1
}
template<class T2>
void F_helper(T2 t, tag2 dummy) {
// Here t is of the type defined by tag2, use different functions of T2
}
// empty structs for overload resolution
struct tag1 {};
struct tag2 {};
// iterator traits
template<class T>
struct iterator_traits {
typedef typename T::category category
// others typedefs
};
// iterators
template<class C, class T>
struct iterator {
typedef C category;
// others typedefs
};
template<class T>
class OneIterator : public iterator<tag1, T> {
// stuff
};
template<class T>
class AnotherIterator : public iterator<tag2, T> {
// stuff
};
template<class T>
class Data {
public:
OneIterator<T> begin() { //stuff }
AnotherIterator<T> end() { // stuff }
};
int main(int, char**) {
Data d;
F(d.begin()); // finally arrives to first F_helper
F(d.end()); // arrives to second F_helper
return 0;
}
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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 ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/09/02 Raw View
Simone BORDET wrote:
>
> Hi,
>
> can someone explain me what are the iterator_traits for ?
> Look for example at the end of page 554 of C++ PL third edition by
> Stroustrup,
> why not use In::iterator_category() instead of
iterator_category is a member typedef, not a member function, and the
class it's typedef'd to won't usually have an accessible constructor, so
there's no need for the '()'.
> iterator_traits<In>::iterator_category() ?
> It seems to me that iterator_traits redefine typedefs already existing in
> iterator...
Not all iterators are classes, so they can't all have typedefs
associated with them. For instance, "T*" is a random-access iterator.
When you have a template parameter that is an iterator type,
iterator_traits provides a mechanism for getting those typedefs, which
works even for "T*". The implementation is required to provide the
following partial specialization:
template<class T> struct iterator_traits<T*> {
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef random_access_iterator_tag iterator_category;
};
And another very similar one for 'const T*'.
---
[ 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 ]
Author: seth@kansmen.com (Seth Jones)
Date: 1999/09/03 Raw View
In article <7qk6hr$aek$1@nslave1.tin.it>, simon@most.it
says...
> Hi,
>
> can someone explain me what are the iterator_traits for ?
> Look for example at the end of page 554 of C++ PL third edition by
> Stroustrup,
> why not use In::iterator_category() instead of
> iterator_traits<In>::iterator_category() ?
> It seems to me that iterator_traits redefine typedefs already existing in
> iterator...
>
> TIA
>
> Simon
>
>
> For those that don't have the book:
> template<class T>
> void F(T t) { // function that I want to use
> return F_helper(t, iterator_traits<T>::iterator_category());
> }
>
>
But an iterator need not be derived from struct
iterator. "Anything that behaves like an iterator is an
iterator."(The C++ Programming Language 3rd ed. p. 550)
For example, an ordinary pointer into an array is a
random-access iterator. You can't define a member
function iterator_category for a pointer, but you can
instantiate iterator_traits for a pointer.
Seth Jones
---
[ 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 ]