It does not work because in {a,b}
you are making a copy of a
and b
. One possible solution would be to make the loop variable a pointer, taking the addresses of a
and b
:
#include <initializer_list>
struct Obj {
int i;
};
Obj a, b;
int main() {
for(auto obj : {&a, &b}) {
obj->i = 123;
}
}
See it live
Note: it is generically better to use auto
, as it could avoid silent implicit conversions
Related Contents:
- How can I loop through a C++ map of maps?
- How can you iterate over the elements of an std::tuple?
- Initializing a member array in constructor initializer
- How to iterate std::set?
- Can I list-initialize a vector of move-only type?
- initializer_list and move semantics
- When to use the brace-enclosed initializer?
- Why isn’t std::initializer_list a language built-in?
- What would a std::map extended initializer list look like?
- std::queue iteration
- Initializing fields in constructor – initializer list vs constructor body [duplicate]
- How to make generic computations over heterogeneous argument packs of a variadic template function?
- C++11 initializer list fails – but only on lists of length 2
- Why does initialization of array of pairs still need double braces in C++14?
- Convert a vector to initializer_list
- Why isn’t `std::initializer_list` defined as a literal type?
- How do I initialize a member array with an initializer_list?
- Can I initialize an STL vector with 10 of the same integer in an initializer list?
- Why does auto x{3} deduce an initializer_list?
- Get index of current element in C++ range-based for-loop
- Why does the number of elements in a initializer list cause an ambiguous call error?
- How does one iterate through an unordered set in C++?
- Brace-enclosed initializer list constructor
- Initialize multiple constant class members using one function call C++
- Why doesn’t `std::initializer_list` provide a subscript operator?
- Unsigned int reverse iteration with for loops
- Initializer lists and RHS of operators
- initializer_list and template type deduction
- How do I iterate over a vector and also know the index of the element?
- Iterating over a vector in reverse direction
- What could go wrong if copy-list-initialization allowed explicit constructors?
- std::initializer_list as function argument
- How to detect a Christmas Tree?
- Throw keyword in function’s signature
- Why does std::list::reverse have O(n) complexity?
- Why does integer overflow on x86 with GCC cause an infinite loop?
- What does extern inline do?
- What is the difference between std::transform and std::for_each?
- Recommended way to initialize srand?
- Unsigned double in C++?
- What is the default value for C++ class members
- Is it possible to pass derived classes by reference to a function taking base class as a parameter
- Set local environment variables in C++
- Fortran vs C++, does Fortran still hold any advantage in numerical analysis these days? [closed]
- Who deletes the memory allocated during a “new” operation which has exception in constructor?
- Why does left shift operation invoke Undefined Behaviour when the left side operand has negative value?
- How are the __cplusplus directive defined in various compilers?
- Why is the sum of an int and a float an int?
- Make a function accepting an optional to accept a non-optional?
- Is a lambda expression a legal default (non-type template) argument?