#include <initializer_list>
struct Obj {
int i;
};
Obj a, b;
int main() {
for(Obj& obj : {a, b}) {
obj.i = 123;
}
}
This code does not compile because the values from the initializer_list
{a, b}
are taken as const Obj&
, and cannot be bound to the non-const reference obj
.
Is there a simple way to make a similar construct work, i.e. iterate over values that are in different variables, like a
and b
here.