Boost logo

Boost Users :

From: abir basak (abirbasak_at_[hidden])
Date: 2006-12-02 04:39:03


Hi,
   I am facing some more range problem from boost::range;
According to the CVS RC_1_34 works. Revision 1.21 version specified by
Thorsten Ottosen , most of the classes work.
What is my interest is to have a get_range function inside some of the
container class like std::vector and boost::circular_buffer (from boost
CVS)
Here I have test class to show the problem I am facing.
First, when I inherit from a vector , and specify a range function just
like iterator functions ( begin() and end() )

template<typename T>
class range_vector : public std::vector<T>{
public:
   typedef boost::sub_range<range_vector> range;
   typedef boost::sub_range<const range_vector> const_range;
   range get_range(size_type from,size_type to){
        std::cout<<"range"<<std::endl;
        return range(begin()+from,begin()+to);
   }
   const_range get_range(size_type from,size_type to)const{
        std::cout<<"const range"<<std::endl;
        return const_range(begin()+from,begin()+to);
   }
};
int main(){
   range_vector<int> v;
   int x[] = {0,1,2,3,4,5,6,7,8};
   v.insert(v.end(),x,x+9);
   std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout," "));
   std::cout<<std::endl;
   range_vector<int>::range r = v.get_range(1,5);
   r[0] = 100;
   copy(r.begin(),r.end(),std::ostream_iterator<int>(std::cout," "));
   std::cout<<std::endl;
   range_vector<int>::const_range cr = v.get_range(1,5);
   copy(cr.begin(),cr.end(),std::ostream_iterator<int>(std::cout," "));
   std::cout<<std::endl;
   const range_vector<int>& cv = v;
   range_vector<int>::const_range cr1 = cv.get_range(1,5);
   copy(cr.begin(),cr.end(),std::ostream_iterator<int>(std::cout," "));
   std::cout<<std::endl;
}
This code compiles perfectly and gives result as expected. It also works
with deque .

However when I compile the same code for boost::circular_buffer,i.e
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;
   range get_range(size_type from,size_type to){
        std::cout<<"range"<<std::endl;
        return range(begin()+from,begin()+to);
   }
   const_range get_range(size_type from,size_type to)const{
        std::cout<<"const range"<<std::endl;
        return const_range(begin()+from,begin()+to);
   }
};
It has a same interface that of a vector, it fails to compile when I
call the non-const get_range function.
i.e
//Initialized the circular_buffer with a few elements.
range_vector<int> v;
   int x[] = {0,1,2,3,4,5,6,7,8};
   v.insert(v.end(),x,x+9);
   std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout," "));
   std::cout<<std::endl;

///got a const reference and invoking const version of get_range. works
fine.
const range_vector<int>& cv = v;
   range_vector<int>::const_range cr1 = cv.get_range(1,5);
   copy(cr1.begin(),cr1.end(),std::ostream_iterator<int>(std::cout," "));
   std::cout<<std::endl;
///now trying to get a non-const version
  range_vector<int>::range r = v.get_range(1,5);
Here it gives error in Visual Studio
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>
         ]

///now trying to get a non-version and assign it to const version
  range_vector<int>::const_range r = v.get_range(1,5);
It gives the same error.
Here C (container) is passed as correct one. begin() and end() are too
executes correct version (non-const one).
Not getting why it won't work.

Any kind of help is highly appreciated.
Thanks to boost community in advance.

abir basak

-- 
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