Re: Alternative to initializer_list of references for temporary referential sequences?
On Friday, January 9, 2015 at 3:00:13 PM UTC+2, Rob Desbois wrote:
Hi all,
I'm trying to come up with a simple solution for creating temporary,
incidental, *referential* sequences.
I want to be able to do something akin to this:
int a{1}, b{2}, c{3};
for (auto&& i : {a, b, c})
i *= 2;
....
An obvious alternative is to store the objects (a, b, c) in some container,
but doing so loses the symbolic meaning of the variable names, which is
more important to readability.
You can have both container and separate variables if you so want.
For example with array of pointers:
#include <iostream>
int main()
{
int a{1}, b{2}, c{3}, *p[]{&a,&b,&c};
for ( auto i : p ) *i *= 2;
std::cout << "a:" << a << " b:" << b << " c:" << c << std::endl;
}
Output:
a:2 b:4 c:6
Works for me.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]