Boost logo

Boost Users :

Subject: Re: [Boost-users] dubious range mismatch prerequisite
From: giecrilj_at_[hidden]
Date: 2011-11-17 07:27:34

Użytkownik Krzysztof Żelechowski napisał:
> The documentation for mismatch, as applied to ranges, says that rng2 must be
> at least as long as rng1. Why is this precondition necessary? The
> precondition is impossible to evaluate for bare input iterators without
> destroying the iterators, whereupon it is impossible to call mismatch any
> more.
> IMHO, it would be perfectly reasonable to stop at the end of rng2 if rng2 is
> shorter.
> <URL:
> doc/html/range/reference/algorithms/non_mutating/mismatch.html>

I am steering towards the following invocation:

::boost:: mismatch (array, :;std:: cin, eq_int_type);

OTOH, it is possible, although more troublesome than it should be, to
use a modified input stream buffer iterator that does returns int_type
and does not fail (Listing#1). However, that begs the question how the
second range argument should be constructed, since the end of the second
range is ignored anyway. My solution, pseudorange, is in Listing#2, and
my invocation becomes (with obvious abbreviations)

::boost:: mismatch
(array | transformed (to_int_type),
pseudorange < iterator_oo > (:;std:: cin), eq_int_type);

Note that the fact that iterator_oo () is a valid end iterator for an
input stream buffer is accidental, and pseudorange < char const * > will
also work provided the length prerequisite is met somehow.

My OP suggests that you are not doing ::boost:: mismatch right; however,
temporarily assuming that the bug is here to stay since noone considered
it worth answering, am I doing it right?


== Listing#1 ==
< class P_C, class P_T = typename ::std:: istreambuf_iterator < P_C > >
class istreambuf_iterator_oo
public: typedef P_T inherited;
public: typedef P_C char_type;
public: typedef typename inherited:: traits_type traits_type_;
public: typedef typename inherited:: int_type int_type_; public: typedef
::std:: iterator
typename inherited:: iterator_category,
int_type_, typename inherited:: difference_type,
typename inherited:: pointer, typename inherited:: reference > iter_;
class I:
public iter_
public: typedef int_type_ int_type;
public: typedef traits_type_ traits_type;
public: typedef typename traits_type:: char_type char_type;
public: static int_type eof (void) { return traits_type:: eof (); }
public: static int_type promote (char_type const &p_c)
{ return traits_type:: to_int_type (p_c); }
public: I (void): it () {}
public: I (inherited const &p_it): inherited (p_it) {}
public: typedef typename inherited:: istream_type istream_type;
public: I (istream_type &p_it): it (p_it) {}
public: operator inherited const & () const { return it; }
public: inherited const & value (void) const { return it; }
operator* (void) const
{ return it == inherited ()? eof (): promote (*it); }
int equal (inherited const &p) const { return it. equal (p); }
I &operator ++ (void) { if (it != inherited ()) ++it; return *this; }
protected: inherited it;
}; };

== Listing#2 ==
template < class P_I >
struct pseudorange
typedef P_I iterator, const_iterator;
iterator begin () const { return m_; }
   iterator end () const { return iterator (); }
iterator m_;
pseudorange (iterator const &p_): m_ (p_) {} };

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at