C++ Template Metaprogramming
Solutions to the exercises throughout the book
|
Why is there no for_each
?
Why is there no for_each
?
6-2. Because std::for_each is the most basic algorithm in the standard library, you may be wondering why we didn't say anything about its compile time counterpart. The fact is that unlike, for example, transform, the algorithm does not have a pure compile time counterpart. Can you offer an explanation for that fact?
Because C++ template metaprogramming has no have side-effects of any kind, the most useful forms of sequence traversal are either transforming one sequence into a copy, or visiting all arrays and accumulating a result. These are covered more concisely by the existing algorithms, without a need for a general purpose array traversal.