Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range based for-loop on array passed to non-main function

When I try to compile the following code in gcc 4.8.2, I get the following error:

test.cc: In function ‘void foo(int*)’:
test.cc:15:16: error: no matching function for call to ‘begin(int*&)’
   for (int i : bar) {
                ^

Along with a bunch of others from deeper in the template library.

#include <iostream>
using namespace std;

void foo(int*);

int main() {
  int bar[3] = {1,2,3};
  for (int i : bar) {
    cout << i << endl;
  }
  foo(bar);
}

void foo(int* bar) {
  for (int i : bar) {
    cout << i << endl;
  }
}

If I redefine foo to use an indexed for loop, then the code compiles and behaves as expected. Also, if I move the range-based output loop into main, I get the expected behaviour as well.

How do I pass the array bar to foo in such a way that it is capable of executing a range-based for-loop on it?

like image 932
castle-bravo Avatar asked Oct 03 '14 16:10

castle-bravo


People also ask

How does a range-based for loop end?

A range-based for loop terminates when one of these in statement is executed: a break, return, or goto to a labeled statement outside the range-based for loop. A continue statement in a range-based for loop terminates only the current iteration. Automatically recognizes arrays. Recognizes containers that have .begin () and .end ().

Which statement terminates the current iteration of a range-based for loop?

A continue statement in a range-based for loop terminates only the current iteration. Automatically recognizes arrays. Recognizes containers that have .begin () and .end ().

What is range based for loop in C++?

Range-based for loop in C++. Range-based for loop in C++ is added since C++ 11. It executes a for loop over a range. Used as a more readable equivalent to the traditional for loop operating over a range of values, such as all elements in a container.

How do you loop through a range in Python?

Python Looping Through a Range Python Glossary. The range() Function To loop through a set of code a specified number of times, we can use the range() function, The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.


2 Answers

Using C++20's std::span, you can have a reference to an array or runtime size.

Here's your code with std::span instead:

#include <iostream>
#include <span>

void foo(std::span<int>);

int main() {
  int bar[3] = {1,2,3};
  for (int i : bar) {
    std::cout << i << std::endl;
  }
  foo(bar);
}

void foo(std::span<int> bar) {
  for (int i : bar) { // now works
    std::cout << i << std::endl;
  }
}
like image 98
Guillaume Racicot Avatar answered Oct 06 '22 01:10

Guillaume Racicot


With the array decaying into a pointer you're losing one important piece of information: its size.

With an array reference your range based loop works:

void foo(int (&bar)[3]);

int main() {
  int bar[3] = {1,2,3};
  for (int i : bar) {
    cout << i << endl;
  }
  foo(bar);
}

void foo(int (&bar)[3]) {
  for (int i : bar) {
    cout << i << endl;
  }
}

or, in a generic fashion (i.e. without specifying the array size in the function signature),

template <std::size_t array_size>
void foo(int (&bar)[array_size]) {
  for (int i : bar) {
    cout << i << endl;
  }
}

Try it out

like image 20
Marco A. Avatar answered Oct 06 '22 01:10

Marco A.