Initializer list in a range for loop

19,632

Solution 1

There is no need to use the verbose std::initializer_list inside the loop

#include <iostream>
#include <initializer_list>

struct B { virtual int fun() { return 0; } };
struct D1 : B { int fun() { return 1; } };
struct D2 : B { int fun() { return 2; } };

int main()
{
    D1 x;
    D2 y;

    B* px = &x;
    B* py = &y;

    for (auto& e : { px, py })
            std::cout << e->fun() << "\n";    
}

Live Example.

If you want to do it on-the-fly without defining px and py, you can indeed use std::initializer_list<B*>{ &x, &y } inside the loop.

Solution 2

You can simply write

for(auto object : {object1, object2, object3}) {
   // work
}
Share:
19,632
Juraj Blaho
Author by

Juraj Blaho

Updated on June 22, 2022

Comments

  • Juraj Blaho
    Juraj Blaho almost 2 years

    I have objects of different types derived from a single super-type. I wonder if there are any disadvantages in using std::initializer list in a range for loop like this:

    for(auto object: std::initializer_list<Object *>{object1, object2, object3}) {
    }
    

    Is it completely OK and efficient or would it be better to use an array? To me the std::array solution seems to be more restrictive for the compiler and there is a disadvantage of explicitly stating the size:

    for(auto object: std::array<Object*, 3>{object1, object2, object3}) {
    }
    

    Is there any other or nicer way of iterating over an explicitly given list of objects?

  • Juraj Blaho
    Juraj Blaho about 10 years
    That does not seem to work for me. As I wrote objects are of different types with a common super-type: ideone.com/4KtpQw
  • Johannes Schaub - litb
    Johannes Schaub - litb about 10 years
    @JurajBlaho try writing {&a, {&b}}
  • Juraj Blaho
    Juraj Blaho about 10 years
    @JohannesSchaub-litb: It only works if at least one element is of the base (target) type, but nice trick anyway.
  • TankorSmash
    TankorSmash almost 9 years
    What is the name of the { obj, obj } thing? Is that just an initializer list, or is there a special name for it?
  • underscore_d
    underscore_d about 8 years
    Of course, this can only be used as-is if (A) objectNs are definitely pointers already (otherwise inefficient, object slicing, only updating a temporary copy of the real element, etc.), and (B) the objects are of a common type as noted above; base/derived combinations need the verbose syntax AFAICT. @TankorSmash It's a braced init-list that can be deduced to various types, but std::initializer_list is given top priority.