Boost logo

Boost Users :

From: abir basak (abirbasak_at_[hidden])
Date: 2006-12-05 00:36:29


Thorsten Ottosen wrote:
> abir basak wrote:
>> abir basak wrote:
>
>
>> To make the problem little clearer,
>> typedef boost::circular_buffer<int> cbi;
>> typedef std::vector<int> vi;
>> This code works,
>> boost::sub_range<vi> get_range(vi& v,int from,int to){
>> return boost::sub_range<vi>(v.begin()+from,v.begin()+to);
>> }
>> But this don't.
>> boost::sub_range<cbi> get_range(cbi& v,int from,int to){
>> return boost::sub_range<cbi>(v.begin()+from,v.begin()+to);
>> }
>
> Try to make the example even smaller so you can post it to the list as a
> complete example that does not require any additional dependencies.
>
> I will try it out then.
>
I found a solution in the mean-time. I had just overlooked the
make_iterator_range function, which serves the purpose.
But still I feels some problem is there with the copy ctor of sub_range
(or may be copy ctor of iterator_range ) with Visual Studio 2003 (7.1)
and 2005 (8.0 ) compiler version. All of the example runs perfectly with
MinGW (GCC mingw build 3.4.5 version).
I am trying to give a compilable example.
1)
#include <iterator>
#include <memory>
#include <vector>
#include <iostream>
#include <boost/circular_buffer.hpp>
#include <boost/range.hpp>

template<typename T>
class range_vector : public boost::circular_buffer<T>{
public:
   typedef boost::sub_range<range_vector> range;
   typedef boost::sub_range<const range_vector> const_range;
   typedef typename boost::circular_buffer<T>::size_type size_type;
   using boost::circular_buffer<T>::begin;
   range_vector(size_type capacity) : boost::circular_buffer<T>(capacity){}
   range get_range(size_type from,size_type to){
        return range(begin()+from,begin()+to);
   }
   const_range get_range(size_type from,size_type to)const{
        return const_range(begin()+from,begin()+to);
   }
};
typedef range_vector<int> rvi;
int main() {
   rvi v(9);
   int x[] = {0,1,2,3,4,5,6,7,8};
   v.insert(v.end(),x,x+9);
   rvi::range r1(v.get_range(1,5));
///get_range causes error in VS 7.1 & 8.0 problem is likely due to
copy-ctor of sub_range. Works fine on MinGW.
}
VS error is
e:\boost_1_33_1\boost\range\iterator_range.hpp(60): error C2440: 'type
cast' : cannot convert from 'boost::range_iterator<C>::type' to
'boost::range_iterator<C>::type'
         with
         [
             C=const boost::sub_range<range_vector<int>>
         ]
         and
         [
             C=range_vector<int>
         ]

Inheriting range_vector from std::vector works fine for both compilers.

2) However changing the range_vector<T> class as below works for both
compiler. Note, here it uses boost::make_iterator_range instead.
template<typename T>
class range_vector : public boost::circular_buffer<T>{
public:
   typedef boost::sub_range<range_vector> range;
   typedef boost::sub_range<const range_vector> const_range;
   typedef typename boost::circular_buffer<T>::size_type size_type;
   using boost::circular_buffer<T>::begin;
   range_vector(size_type capacity) : boost::circular_buffer<T>(capacity){}
   range get_range(size_type from,size_type to){
        return boost::make_iterator_range(begin()+from,begin()+to);
   }
   const_range get_range(size_type from,size_type to)const{
        return boost::make_iterator_range(begin()+from,begin()+to);
   }
};
Not sure if it is an error. You can check if that one is expected
behavior. But it works with make_iterator_range, which I had overlooked.
Thanks for listening.
abir
> -Thorsten

-- 
Abir Basak, Member IEEE
Software Engineer, Read Ink Technologies
B. Tech, IIT Kharagpur
email: abir_at_[hidden]
homepage: www.abirbasak.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net