Consider the following program:
#include <iostream>
#include <algorithm>
#include <numeric>
#include <array>
#include <range/v3/view/transform.hpp>
int main() {
using container = std::array<std::tuple<int,float,double>, 4>;
container tuples {{
{1, 4.f, 8.},
{2, 5.f, 9.},
{3, 6.f, 10.},
{4, 7.f, 11.}
}};
auto int_view =
tuples | ranges::view::transform( [](auto& t){return std::get<int>(t);} );
// int_view[1] = 3; // (*)
auto x = std::accumulate(int_view.begin(), int_view.end(), 0);
std::cout << "x = " << x << std::endl;
}
This compiles and prints 10
; but - if I uncomment the (*)
line - it doesn't compile, with GCC complaining about the left side of the equality not being an lvalue. I was kind of disappointed by that - I was sort of hoping the transformation would produce int&
's which I could assign to...
Is there something I can to make this a modifiable view? Or some other mechanism in the ranges library which would allow me the equivalent of a modifiable view?