Question

It would seem that the "for each" style of syntax available in C++11 permits array iteration without knowledge of the actual size of the array (number of elements). I assume, since it is part of the new standard, that this is perfectly safe, even for C arrays. Typically, you must also separately know the size of a C array before manipulating it, but I want verification from anyone experienced with this new C++ technique that it works exactly as you'd expect:

extern float bunch[100];

for (float &f : bunch) {
  f += someNumber;
}

Is there anything I should know about non-obvious side effects or disadvantages to this technique? It doesn't show much in code I see, probably because most of the code was written before this was in the standard. Want to make sure its rare usage isn't because of some other reason not well-known.

Was it helpful?

Solution

There is nothing strange or unsafe about that usage. The array's size is known at compile time, so it can be used in the loop. This is an example of a template function that allows you to find out the length of an array:

template< class T, std::size_t N >
std::size_t length( const T (&)[N] )
{
  return N;
}

Foo f[5];
std::cout << length(f) << "\n";

This should make it clear that you cannot use this technique, or range based loops, on dynamically sized C-style arrays.

Of course, if you have range based loops then you should also have std::array (if not, you can probably get ti from std::tr1 or boost), so you can avoid the C-style array entirely:

extern std::array<float, 100> bunch;

for (auto &f : bunch) {
  f += someNumber;
}

OTHER TIPS

It is perfectly safe to use a range-based for-loop with arrays. I suppose you're worried that you might accidentally use it on a pointer:

float* array = new float[100];
for (float& f : array) {
    // ...
}

Don't worry though. The compiler will produce an error in this case. So in cases where it's not safe, you'll get a compilation error anyway.

Arrays can be passed as references and have their type and length deduced.

#include <iostream>

template<typename T, size_t N>
void fun(T const (&arr)[N])
{
    for (std::size_t i = 0; i < N; ++i)
       std::cout << arr[i] << " ";
}

int main()
{
   int x[] = { 1, 2, 3 }; // no need to set length here
   fun(x); // 1 2 3, no need to specify type and length here
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top