title: Protected iteration through custom container
body: <p>I want to make a container class that should be allowed to be iterable through a for each loop, but only through for each loops. I don't want to grant access to its .begin() and .end() methods.</p>
  
  <p>Is such a thing possible, maybe through overloading and friendship of std::begin and std::end methods?</p>
  
  <p>I've made some attempts, one of which looks something like the following. The compiler has always complained about the privacy of .begin() and .end(), though.</p>
  
  <pre><code>namespace std {
  
      MyIter begin (MySealedContainer&amp;);
      MyIter end (MySealedContainer&amp;);
  
  }
  
  class MyIter {
      // ...
  };
  
  class MySealedContainer {
      friend MyIter std::begin (MySealedContainer&amp;);
      friend MyIter std::end (MySealedContainer&amp;);
  
      private:
          MyIter begin();
          MyIter end();
  
      // ...
  };
  
  // ---
  
  MyIter std::begin (MySealedContainer&amp; c) {
      return c.begin();
  }
  
  MyIter std::end (MySealedContainer&amp; c) {
      return c.end();
  }
  </code></pre>
  
  <p>Even with a private .begin() and .end(), I gotta be able to do the following:</p>
  
  <pre><code>MySealedContainer foo;
  // Insert elements...
  
  for (auto&amp; each: foo) {
      // Do something with each.
  }
  </code></pre>