Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2002-05-17 05:45:28


Peter Dimov wrote:
>
> From: "Daniel Frey" <daniel.frey_at_[hidden]>
> >
> > 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();
}

Now we have several version that should work anywhere, but what is the
best one considering efficiency?

Regards, Daniel

--
Daniel Frey
aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de

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