I would like to perform the same operation on several arrays, something like:
#include<vector>
#include<algorithm>
int main(void){
std::vector<double> a, b;
for(auto& ab:{a,b}) std::sort(ab.begin(),ab.end()); // error
}
This code fails, since auto& is a const-reference. Is there an elegant way around it?
I think the issue is that it’s a bit like binding a temporary to a non-const reference. There is no “concrete” collection there so it is a bit like a temporary.
IF you have a temporary vector there, it will bind to a const reference but not a non-const one.
I also think this won’t ever work what you are doing, but this should work:
and auto may work too.