title: Why didn't std::for_each + lambda work as expected?
body: <p>It's an exercise from <em>C++ Primer 5th Edition</em>:</p>
  
  <blockquote>
    <p>Exercise 14.7: Define an output operator for you <strong>String</strong> class you
    wrote for the exercises in § 13.5 (p. 531).(Page 558)</p>
  </blockquote>
  
  <p>The <code>string.h</code> I wrote for previous exercises:</p>
  
  <pre><code>/**
   * @brief std::string like class without template
   *
   *        design:
   *
   *        [0][1][2][3][unconstructed chars][unallocated memory]
   *        ^           ^                    ^
   *        elements    first_free           cap
   */
  class String
  {
      friend  std::ostream&amp; operator &lt;&lt;(std::ostream&amp; os, const String&amp; s);
  
  public:
      //! default constructor
      String();
  
      //! constructor taking C-style string i.e. a char array terminated with'\0'.
      explicit String(const char * const c);
  
      //! copy constructor
      explicit String(const String&amp; s);
  
      //! move constructor    --07.Jan.2014
      String(String&amp;&amp; s) noexcept;
  
      //! operator =
      String&amp; operator = (const String&amp; rhs);
  
      //! move operator =     --07.Jan.2014
      String&amp; operator = (String&amp;&amp; rhs) noexcept;
  
      //! destructor
      ~String();
  
      //! members
      char* begin() const  { return elements;   }
      char* end()   const  { return first_free; }
  
      std::size_t size()     const {return first_free - elements;  }
      std::size_t capacity() const {return cap - elements;         }
  
  private:
  
      //! data members
      char* elements;
      char* first_free;
      char* cap;
  
      std::allocator&lt;char&gt; alloc;
  
      //! utillities for big 3
      void free();    
  };
  
  std::ostream&amp;
  operator &lt;&lt; (std::ostream&amp; os, const String&amp; s);
  </code></pre>
  
  <p>Part of the <code>string.cpp</code>:</p>
  
  <pre><code>//! constructor taking C-style string i.e. a char array terminated with'\0'.
  String::String(const char *  const c)
  {
      auto p = c;
      char* newData = alloc.allocate(sizeof(p));
  
      std::uninitialized_copy(p, (p + sizeof(p)), newData);
  
      //! build the data structure
      elements = newData;
      cap = first_free = newData + sizeof(c);
  }
  
  std::ostream &amp;operator &lt;&lt;(std::ostream &amp;os, const String &amp;s)
  {
      std::for_each(&amp;s.elements, &amp;s.first_free, [&amp;](const char* p){
          os &lt;&lt; *p;
      });
  
      return os;
  }
  </code></pre>
  
  <p><code>main.cpp</code>:</p>
  
  <pre><code>#include "string.h"
  #include &lt;iostream&gt;
  int main()
  {
      String s("1234");
      std::cout &lt;&lt; s &lt;&lt;"\n";
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>1 
  Press &lt;RETURN&gt; to close this window...
  </code></pre>
  
  <p>Why is the output like so? why not <code>1234</code>?</p>