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?
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 ().
A continue statement in a range-based for loop terminates only the current iteration. Automatically recognizes arrays. Recognizes containers that have .begin () and .end ().
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.
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.
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;
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With