Boost logo

Boost Users :

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


Thorsten Ottosen wrote:
> abir basak wrote:
>> 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>
>
> Hm... this is not legal.
It is "not legal" or "not preferred" ? I think it is legal, as I have a
statement like VI v(10); rather than VI v; copy doesn't grow memory, or
change size. But here I am just copying! Of course the other (and better
  ) way is,
VI v;
int data[] = {0,1,2,3,4,5,6,7,8,9};
v.assign(data,data+10);
>
> May I suggest
>
> v = boost::assign::list_of(0)(1)(2)(3)(4)(5)(6) ... ;
>
> ?
>
>> ///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 ?
>
> This is an error in the range lib that should be fixed in the upcoming
> version of boost.
Is the correction is in the CVS head ? Or can you suggest what should be
the correction,
if it is a small correction?
Thanks for answering.
abir
> -Thorsten


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