I am new to c++ and am trying to understand how for_each works
If you meant the range-based for
-loop as for_each here, it just syntactic sugar for the iterator based loop, which works for any objects for which begin and end member functions defined.
auto&& __range = range_expression; // (until C++17)
for (auto __begin = begin_expr, __end = end_expr; __begin != __end; ++__begin)
{
range_declaration = *__begin;
loop_statement
}
The begin and end iterators for an array(e.g. int a[size]
) can easily be found by a
and a + size
respectively, as iterators are generalizations of pointers.
Therefore, whenever iterate through an array(in a range-based for
-loop), the compiler should be knowing the size
of the underlying array.
In your version, the passed array will be deduced to int*
(no size information available) instead of the actual type int a[5]
.
There are few options, by which the passed array can be deduced to the actual type:
- Option - 1: Provide a non - type parameter for size of the array,
with the following syntax.
template <typename T, std::size_t N>
void forEachTest(T const (&a)[N])
// ^^^^^^^^^^^^^^^^
{
for (int x : a)
std::cout << x << std::endl;
}
- Option - 2: pass it by const - reference, since the values are not
being modified inside the function.
template <typename T>
void forEachTest(T const (&a))
// ^^^^^^^^^^^^
{
for (int x : a)
std::cout << x << std::endl;
}
- Option - 3: forward the array perfectly with forwarding reference.
template <typename T>
void forEachTest(T&& a)
// ^^^^^^
{
for (int x : a)
std::cout << x << std::endl;
}