title: for loop vs std::for_each with lambda
body: <p>Let's consider a template function written in C++11 which iterates over a container.
  Please exclude from consideration the range loop syntax because it is not yet supported by the compiler I'm working with.</p>
  
  <pre><code>template &lt;typename Container&gt;
  void DoSomething(const Container&amp; i_container)
    {
    // Option #1
    for (auto it = std::begin(i_container); it != std::end(i_container); ++it)
      {
      // do something with *it
      }
  
    // Option #2
    std::for_each(std::begin(i_container), std::end(i_container), 
      [] (typename Container::const_reference element)
      {
      // do something with element
      });
    }
  </code></pre>
  
  <p>What are pros/cons of for loop vs <code>std::for_each</code> in terms of:</p>
  
  <p>a) performance? (I don't expect any difference)</p>
  
  <p>b) readability and maintainability?</p>
  
  <p>Here I see many disadvantages of <code>for_each</code>. It wouldn't accept a c-style array while the loop would. The declaration of the lambda formal parameter is so verbose, not possible to use <code>auto</code> there. It is not possible to break out of <code>for_each</code>.</p>
  
  <p>In pre- C++11 days arguments against <code>for</code> were a need of specifying the type for the iterator (doesn't hold any more) and an easy possibility of mistyping the loop condition (I've never done such mistake in 10 years).</p>
  
  <p>As a conclusion, my thoughts about <code>for_each</code> contradict the common opinion. What am I missing here?</p>