2

Working on a Kubuntu 14.04 system with gcc 4.8.4 I ran into the following problem:

Using std:vector, I can assign between vector elements via an iterator:

std::vector<float> v ;
v.push_back(0.0) ;
v.push_back(1.0) ;
auto vv = v.begin() ;
vv[0] = vv[1] ;
assert ( v[0] == v[1] ) ;

Using a boost multi_array, this fails:

typedef boost::multi_array<float, 1> array_type; 
boost::array<array_type::index, 1> shape = {{ 2 }};
array_type a(shape) ;
a[0] = 0.0 ;
a[1] = 1.0 ;
auto aa = a.begin() ;
aa[0] = aa[1] ;
assert ( a[0] == a[1] ) ; // fails, a[0] is unmodified

I can work around this using a different idiom like

aa[0] = *(aa+1) ;

But the code I want to use with the multi_array is written using assignments of the type that doesn't work. What am I missing?

1
  • It is 2024 and this is still a bug in Boost.MultiArray. I tried a similar library which doesn't have the bug. godbolt.org/z/4W5o1jG8r Commented Jan 7, 2024 at 18:15

1 Answer 1

1

The reason is that the iterator involved in operator[] for boost::multi_array is an input iterator, which is not required to be mutable.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, my comment was complete nonsense! (Though input iterators really don't require op-[] to exist.)
Indeed. The boost docu states that 'This is an iterator over the values of A. If NumDims == 1, then it models Random Access Iterator. Otherwise it models Random Access Traversal Iterator, Readable Iterator, Writable Iterator, and Output Iterator.' So in this case it would be a Random Access Iterator.
The OP code should work. If it didn’t it was a bug in BMA
You pulled the wrong documentation. mulit_array fulfills the concept of MutableMultiArray which, in this sense is more than a Collection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.