1

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?

Kay F. Jahnke
  • 371
  • 2
  • 7

1 Answers1

2

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

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Sorry, my comment was complete nonsense! (Though input iterators really don't require op-`[]` to exist.) – Kerrek SB Jan 04 '16 at 10:21
  • 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. – Kay F. Jahnke Jan 04 '16 at 10:41