title: Is Iterable-like behavior in C++ attainable?
body: <p>Java allows classes to reveal <code>Iterable</code> types so clients can traverse some instance's collection of data, like so:</p>
  
  <pre><code>public class MyClass
  {
      private ArrayList&lt;String&gt; strings;
      private ArrayList&lt;Integers&gt; ints;
      public MyClass() { /* generate data ... */ }
      public Iterable&lt;String&gt; allStrings() {return strings;}
      public Iterable&lt;Integer&gt; allInts() {return ints;}
  }
  </code></pre>
  
  <p>This has always struck me as "clean" because it maintains encapsulation, allowing me to change the <code>ArrayList</code>s to <code>LinkedList</code>s if I wanted to and is still convenient to the client in constructs such as <code>for(String s : myClassInstance.allStrings()) //...</code>.</p>
  
  <p>In C++, however, if I want to allow the client to use my <code>for-loop</code>, in absence of an <code>Iterable</code>, I need to return a <code>const vector&lt;T&gt;&amp;</code> or whatever, which is obviously not too great.</p>
  
  <p>Defining <code>template&lt;&gt; begin&lt;my_class&gt; {/*...*/}</code> and friends is nice, but only if <code>my_class</code> has one collection to iterate over. What else can I do?</p>