|
Boost : |
From: Damien Fisher (damien_at_[hidden])
Date: 2002-05-17 08:15:04
On Fri, 17 May 2002, Daniel Frey wrote:
> > > template< typename Container >
> > > bool is_single_element( const Container& container )
> > > {
> > > // Resolved at compile-time:
> > > if( ::boost::is_pointer< Container::iterator >::value )
> > > return !container.empty() &&
> > > boost::next( container.begin() ) == container.end();
> > > else
> > > return !container.empty() &&
> > > ++container.begin() == container.end();
> > > }
> >
> > template<class It> bool is_single_element(It first, It last)
> > {
> > return first != last && ++first == last;
> > }
>
> How does this help to prevent unneccessary temporaries? You just moved
> them to the parameter list, don't you?
>
> template< class Iterator >
> bool is_single_element( Iterator first, const Iterator& last )
> {
> return first != last && ++first == last;
> }
>
> template< class Container >
> bool is_single_element( const Container& container )
> {
> return !container.empty() &&
> ++Container::const_iterator( container.begin() ) ==
> container.end();
> }
I know was the one who wrote it, but I personally don't like the one
taking a container :).
Aren't iterators generally supposed to be "cheap" to copy? Anyway, even
if they aren't, one could use boost::call_traits for the 2nd parameter.
(I've always wondered about whether this "cheap" copying is actually
required of a standard iterator or is just good practice, so I'd
appreciate clarification from someone as to whether I am just making
things up or not.)
When I get some time (hopefully this weekend) I'll write up distance_n
(which I personally find more appealing than is_single_element as it is
more STL-ish in its naming).
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk