Indexing map of const vector results in value instead of const reference. Intended or bug?
Version: 3.3.7
Platform: 64-bit Windows
It might be a duplicate but I could not find an issue that describes this. Currently, when I have a map of a constant vector type, the return type for the indexing operation of the map becomes a value type instead of a const reference type. This makes the following example not compile.
VectorXd v1(6);
v1 << 1.0, 2.0, 3.0, 4.0, 5.0, 6.0;
const auto vm = Vector3d::Map(&v1[1]);
const auto vmc = Vector3d::Map(&as_const(v1[1]));
cout << &v1(1) << endl; // okay, pointer to "2.0"
cout << &vm(1) << endl; // okay, pointer to "3.0"
cout << &vmc(1) << endl; // error, vmc(1) has type "double" instead of "const double&"
Although I can still get the pointer using vmc.data() + 1
, the behavior really got me as a surprise. As a comparison with a standard type that does something similar to Map
, an std::span
of a constant type, when indexed, gives a const reference instead of a value.
My questions are:
- Is this behavior intended or a bug?
- If it is intended: is there any reason for this behavior (I thought of striding, alignment, etc, but I cannot convince myself)? If it is a bug: is it possible to correct it?
Thanks!