Boost logo

Boost :

From: Pavol Droba (droba_at_[hidden])
Date: 2003-10-23 13:41:48


On Thu, Oct 23, 2003 at 12:51:41PM -0400, David Abrahams wrote:
> Beman Dawes <bdawes_at_[hidden]> writes:
>
> > At 10:47 AM 10/23/2003, Pavol Droba wrote:
> > >On Thu, Oct 23, 2003 at 11:28:38PM +1000, Thorsten Ottosen wrote:
> > >> I would say we have to provide
> > >>
> > >> template<typename InputContainerT, typename SearchContainerT>
> > >> iterator_range< typename string_algo::container_const_iterator<
> > >> InputContainerT >::type >
> > >> find_first(const InputContainerT &, const SearchContainerT &);
> > >>...
> > >
> > >Yes you have overlooked one important point. Your second proposed variant
> > >does not make sense and what worse, it is even very dangerous.
> > >
> > >find_xxx algorithms return a reference into the input container by the
> > >means
> > >of iterators. Using a temporary as an input, you would get an invalid
> > >reference as a result.
> > >
> > >Returning a reference from a temporary is quite a big error in my
> > opinion.
> >
> > What is the fix? There really needs to be some convenient way to do a
> > find_first on a const container. Provide a version that takes begin
> > and end iterators? Or is that already done by one of the algorithms in
> > detail that you are going to move into boost::algorithm::string?
>
> I'm not sure a fix is needed. The first variant will already match
> const containers, as long as they're not temporaries.
>
>
> template <class T>
> int f(T& x);
>
> int const x;
> int y = f(x); // T deduced as const int.
>

You are right, there is no restriction on const, rather ther is a restriction,
that the input must be valid R-value i.e. not a temporary.

I think this is absolutely correct behaviour.

Pavol


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