Kay F. Jahnke
Kay F. Jahnke

Reputation: 391

assignment between array elements using boost multi_array iterator

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?

Upvotes: 2

Views: 273

Answers (1)

user5743044
user5743044

Reputation: 19

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

Upvotes: 1

Related Questions