Topic: inferring dereference return type for input iterator


Author: cbarron3@ix.netcom.com (Carl Barron)
Date: Mon, 25 Nov 2002 19:14:52 +0000 (UTC)
Raw View
John Potter <jpotter@falcon.lhup.edu> wrote:

> On Sun, 24 Nov 2002 00:12:25 +0000 (UTC), kuyper@wizard.net ("James
> Kuyper Jr.") wrote:
>
> > Kevin S. Van Horn wrote:
> > ....
> > > What is one to put for the return type of
> > > wrapped_input_iter<InputIter>::operator*() above?  I would like it to
> > > be the same as the return type for *i when i has type InputIter.  But
> > > what is this type?  ...
>
> >     std::iterator_traits<InputIter>::reference
>
> There is no such requirement in the standard.
>
> > > ... If i is a Forward Iterator then the type of *i is
> > > presumably the reference type (though I don't know of anything in the
> > > standard that guarantees this),  ...
>
> > Table 74.
>
> Table 74 says that *a gives T&.  There is absolutly nothing anywhere
> in the standard that relates iterator_traits pointer and reference to
> anything related to value_type.  In fact, there is nothing that says
> that reference and pointer must exist for an iterator.
> Iterator_traits may assume and use that, but nothing requires it.
>
   For an input iterator table 72 states that the result *iterator is
convertable to T.  Table 74 is not relavent to input iterators.

    Section 24.3.1 states only that std::iterator_traits<Iterator> must
define the iterator_category,difference_type and value_type, for an
output iterator difference_type and value_type are void.  Ussually
std::iteraotr<> is used to construct the types for iterator_traits and
iterator<> has default template arguments makeing reference &value_type
and pointer *value_type.  I think it is a defect myself and should
requrie reference and pointer [setting them to void if they are not
relavant] but that is another story...

Input iterators are the only proxiable operator *() avalable in the
standard [table 74 applies to Forward or more enhanced iterator and none
of these can be have operator *() return anything but T & or const T &]
 amd I think that is another defect....





---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kuyper@wizard.net ("James Kuyper Jr.")
Date: Tue, 26 Nov 2002 03:52:21 +0000 (UTC)
Raw View
Carl Barron wrote:
> John Potter <jpotter@falcon.lhup.edu> wrote:
>
>
>> On Sun, 24 Nov 2002 00:12:25 +0000 (UTC), kuyper@wizard.net ("James
>> Kuyper Jr.") wrote:
....
>>>>... If i is a Forward Iterator then the type of *i is
>>>>presumably the reference type (though I don't know of anything in the
>>>>standard that guarantees this),  ...
>>>
>>>Table 74.
>>
>>Table 74 says that *a gives T&.  There is absolutly nothing anywhere
>>in the standard that relates iterator_traits pointer and reference to
>>anything related to value_type.  In fact, there is nothing that says
>>that reference and pointer must exist for an iterator.
>>Iterator_traits may assume and use that, but nothing requires it.
>>
>
>    For an input iterator table 72 states that the result *iterator is
> convertable to T.  Table 74 is not relavent to input iterators.

It is, however, relevant to this particular question, because this
particular question was about forward iterators.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kuyper@wizard.net ("James Kuyper Jr.")
Date: Sun, 24 Nov 2002 00:12:25 +0000 (UTC)
Raw View
Kevin S. Van Horn wrote:
....
> What is one to put for the return type of
> wrapped_input_iter<InputIter>::operator*() above?  I would like it to
> be the same as the return type for *i when i has type InputIter.  But
> what is this type?  ...

 std::iterator_traits<InputIter>::reference


> ... If i is a Forward Iterator then the type of *i is
> presumably the reference type (though I don't know of anything in the
> standard that guarantees this),  ...

Table 74.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jpotter@falcon.lhup.edu (John Potter)
Date: Sun, 24 Nov 2002 19:47:28 +0000 (UTC)
Raw View
On Sun, 24 Nov 2002 00:12:25 +0000 (UTC), kuyper@wizard.net ("James
Kuyper Jr.") wrote:

> Kevin S. Van Horn wrote:
> ....
> > What is one to put for the return type of
> > wrapped_input_iter<InputIter>::operator*() above?  I would like it to
> > be the same as the return type for *i when i has type InputIter.  But
> > what is this type?  ...

>  std::iterator_traits<InputIter>::reference

There is no such requirement in the standard.

> > ... If i is a Forward Iterator then the type of *i is
> > presumably the reference type (though I don't know of anything in the
> > standard that guarantees this),  ...

> Table 74.

Table 74 says that *a gives T&.  There is absolutly nothing anywhere
in the standard that relates iterator_traits pointer and reference to
anything related to value_type.  In fact, there is nothing that says
that reference and pointer must exist for an iterator.
Iterator_traits may assume and use that, but nothing requires it.

John

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kuyper@wizard.net ("James Kuyper Jr.")
Date: Mon, 25 Nov 2002 05:11:57 +0000 (UTC)
Raw View
John Potter wrote:
> On Sun, 24 Nov 2002 00:12:25 +0000 (UTC), kuyper@wizard.net ("James
> Kuyper Jr.") wrote:
>
>
>>Kevin S. Van Horn wrote:
>>....
>>
>>>What is one to put for the return type of
>>>wrapped_input_iter<InputIter>::operator*() above?  I would like it to
>>>be the same as the return type for *i when i has type InputIter.  But
>>>what is this type?  ...
>>
>
>> std::iterator_traits<InputIter>::reference
>
>
> There is no such requirement in the standard.

You're right - the standard requires the existence of
std::interator_traits<InputIter>::reference only where InputIter is T*.
However, 24.3.1p1 does require that
std::iterator_traits<Iterator>::value_type be defined as the actual
value_type of the iterator, for all iterators (a fact that should be
cross-referenced somewhere in section 24.1, since a user-defined
iterator that isn't derived from std::iterator can meet that requirement
only by having a user-defined specialization of std::iterator_traits).
Therefore, I'll correct my original answer to:

 std::iterator_traits<InputIter>::value_type &


>>>... If i is a Forward Iterator then the type of *i is
>>>presumably the reference type (though I don't know of anything in the
>>>standard that guarantees this),  ...
>>
>
>>Table 74.
>
>
> Table 74 says that *a gives T&.  There is absolutly nothing anywhere
> in the standard that relates iterator_traits pointer and reference to
> anything related to value_type.  In fact, there is nothing that says
> that reference and pointer must exist for an iterator.
> Iterator_traits may assume and use that, but nothing requires it.

I assumed that by "the reference type" Kevin meant nothing more or less
than "T&", which most certainly is a reference type, and is (at least in
this context) "THE reference type".

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Kevin_VanHorn@ndsu.nodak.edu (Kevin S. Van Horn)
Date: Tue, 19 Nov 2002 22:21:38 +0000 (UTC)
Raw View
The Standard seems to have a big hole as far as iterator traits go:
although it specifies what the typedefs provided by iterator_traits<I>
must be in terms of the corresponding typedefs in I, where I is an
iterator class, nowhere does it give requirements on the I typedefs.
In particular, it does not explicitly state any requirements on the
reference type, not even to require it to actually be a reference.
Also, there seems to be no way of knowing what is the actual type
returned by *i where i is an input iterator.

I bring this up because of a problem in defining iterator adaptors, as
in the Boost Iterator Adaptor Library (www.boost.org).  As an example,
suppose that you want to wrap an input iterator in a way that adds
debugging facilities, producing a new input iterator as the result.
So you have something like this:

template <type InputIter>
class wrapped_input_iter
{
  InputIter iter;
public:
  wrapped_input_iter(InputIter const & it)
    : iter(it) { ... }

  ...

  ??? operator*() const
  {
    ... do some debug checks ...
    return *it;
  }
};

What is one to put for the return type of
wrapped_input_iter<InputIter>::operator*() above?  I would like it to
be the same as the return type for *i when i has type InputIter.  But
what is this type?  If i is a Forward Iterator then the type of *i is
presumably the reference type (though I don't know of anything in the
standard that guarantees this), but for an input iterator I only know
that the type of *i is convertible to the value type.

One possible resolution that I have seen is to assume that the type of
*i is always the reference type for the iterator type.  This seems
somewhat plausible, but again I find nothing in the Standard to
support such an assumption.

Have I missed something in the Standard that resolves these questions?
 If not, are there any proposed changes to the Standard to handle such
questions?

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]