Opened 11 years ago

Closed 10 years ago

#1842 closed Bugs (fixed)

boost::sub_rage::front error

Reported by: ka3a4ok <bismuth@…> Owned by: Thorsten Ottosen
Milestone: Boost 1.36.0 Component: range
Version: Boost 1.35.0 Severity: Problem
Keywords: sub_range front back reference Cc:

Description

I think, there is an error in sub_range::front const method. It use const value_type& instead of const reference.

For example, here is a well compiled code, that fails on last assert, but should not:

#include <list>

#include <boost/assign.hpp>
#include <boost/assert.hpp>
#include <boost/range/sub_range.hpp>
#include <boost/iterator/transform_iterator.hpp>

//------------------------------------------------------------------------------
struct Trans:
	public std::unary_function<int, int>
{
	int operator()(const int& a)const
	{
		return a*2;
	}
};
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
	using namespace boost::assign;

	const std::list<int> a=list_of(10);
	const std::list<int> b=list_of(20);

	BOOST_ASSERT(!a.empty() && !b.empty());
	BOOST_ASSERT(a.front()!=b.front());
	BOOST_ASSERT(Trans()(a.front())!=Trans()(b.front()));

	typedef 
		boost::transform_iterator<Trans, std::list<int>::const_iterator>
		my_iter_t;

	typedef 
		std::pair
		<	my_iter_t,
			my_iter_t
		>
		my_pair_t;

	const boost::sub_range<my_pair_t> ar
	(	boost::make_transform_iterator(boost::const_begin(a), Trans()),
		boost::make_transform_iterator(boost::const_end(a), Trans())
	);

	const boost::sub_range<my_pair_t> br
	(	boost::make_transform_iterator(boost::const_begin(b), Trans()),
		boost::make_transform_iterator(boost::const_end(b), Trans())
	);

	BOOST_ASSERT(ar.front()!=br.front()); // <---- fail

	return 0;
}

Change History (1)

comment:1 Changed 10 years ago by Thorsten Ottosen

Resolution: fixed
Status: newclosed

I've commputed the return value in a different manner now. Should fix your problem.

-Thorsten

Note: See TracTickets for help on using tickets.