In a naive attempt to execute the same manipulation on several variables of the same type, I tried the following syntax (with C++20):
int var1, var2, var3;
for(auto& x : {var1, var2, var3})
x += 1;
Unfortunately this does not compile as the type of x
is const int&
rather than int&
which I hoped for. I could work around using
int var1, var2, var3;
for(auto px : {&var1, &var2, &var3})
(*px) += 1;
but that does not seem very pretty. Is there a convenient way that I overlooked? I'm looking for something similarly easy to type and read as the first snippet, where the body of the for loop could just use an l-value reference.