3
for(auto i: {{1,2,3}, {4,5,6}, {7,8,9}}){
    /* loop body */
}

I know I have other ways to get my work done. But I was just wondering why we cannot use such type of list in this loop.

It is giving me this error:

cannot use type 'void' as a range
cigien
  • 57,834
  • 11
  • 73
  • 112
sparsh goyal
  • 89
  • 1
  • 7

1 Answers1

4

{..} has no types, so it is problematic for deduction needed for inner type, you might help by explicitly provide the type: (I used CTAD from C++17 here, before add <int>)

for (auto i: {std::initializer_list{1,2,3}, {4,5,6}, {7,8,9}}){
    /* loop body */
}

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302