Boost logo

Boost Users :

From: abir basak (abirbasak_at_[hidden])
Date: 2006-11-17 01:09:43


Hi,
   I am not sure if it is the proper newsgroup to get answers
    related to boost usage.
   I am facing a problem with boost::sub_range for a const container
   which is not allowing me to access the indexed operator.
   Below a short program to demonstrate the problem. Necessary headers
   are included. Boost version 1.33.1
   typedef vector<int> VI;
   VI v(10);
   int data[] = {0,1,2,3,4,5,6,7,8,9};
   copy(data,data+10,v.begin());///inserted a few int to std::vector<int>
   ///prints 0 1 2 3 4 5 6 7 8 9
   copy(v.begin(),v.end(),ostream_iterator<int>(cout," ")); cout<<endl;
   const VI& cv = v; /// a const reference to the container.
   VI::iterator it = v.begin(); ///got an random access iterator
   it[1] = 100; /// index operator allows to change it
   copy(v.begin(),v.end(),ostream_iterator<int>(cout," ")); cout<<endl;
   ///prints 0 100 2 3 4 ...
   VI::const_iterator it1 = v.begin(); ///got a const_iterator
   cout<<it1[1]<<endl; /// returns const_reference, assignment is not
allowed, access is allowed. prints 100
boost::sub_range<VI> r(v.begin()+2,v.begin()+8); ///got a sub_range
   ///prints 2 3 4 5 6 7
   copy(r.begin(),r.end(),ostream_iterator<int>(cout," "));
   cout<<endl; r[2] = 400; cout<<r[2]<<endl;/// prints 400
   ///prints 2 3 400 5 6 7
   copy(r.begin(),r.end(),ostream_iterator<int>(cout," "));
   ///so far everything is fine. Here problem starts
   /// a const sub_range ?
   boost::sub_range<const VI> r1(cv.begin()+2,cv.begin()+8);
   ///prints 2 3 400 5 6 7
   copy(r1.begin(),r1.end(),ostream_iterator<int>(cout," "));cout<<endl;
   ///next statement is don't compile
   cout<<r1[2]<<endl; ///=> not allowed
   gives error under Visual Studio 7.1
cannot convert from 'const std::allocator<_Ty>::value_type' to
'boost::iterator_range<IteratorT>::value_type &'
  Why it tried to convert it to value_type& instead of const value_type& ?
cout<<typeid(r1.begin()).name()<<endl; says r1.begin() is a
const_iteartor. So the statement cout<<r1[2]<<endl; should return a
const_reference.
  Am I mis interpreting the const version of sub_range ? If so, what is
a const counterpart of sub_range, which behaves just like a pair of
const_iterator ?
  Any help is appreciated.
  Thanks
abir


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