title: How does begin() know which return type to return (const or non-const)?
body: <p>This works perfectly :</p>
  <pre><code>list&lt;int&gt; l;
  list&lt;int&gt;::const_iterator it;
  it = l.begin();
  list&lt;int&gt;::iterator it2;
  it2 = l.begin();
  </code></pre>
  <p>What I don't get is how the <code>list</code> &quot;knows&quot; that it must return the <code>iterator begin()</code> version or the <code>const_iterator begin() const</code> one.</p>
  <p>I'm trying to implement iterators for my container (a trie) and I'm running into this problem. Isn't C++ supposed not to handle differentiation by return type (except when using weird tricks)?</p>
  <p>Here is some code and the compiler error I get :</p>
  <p>My <code>Trie&lt;T&gt;</code> is a templated trie that can contain any type. I have a <code>Trie&lt;int&gt;::iter</code> non-const iterator and a <code>Trie&lt;int&gt;::const_iter</code> const iterator. <code>iter begin()</code> and <code>const_iter begin() const</code> are declared (and defined) in the Trie class.</p>
  <pre><code>Trie&lt;int&gt; t;
  Trie&lt;int&gt;::const_iter it;
  it = t.begin();
  </code></pre>
  <p>Error :</p>
  <pre><code>../test/trie.cpp:181: error: no match for 'operator=' in 'it = Trie&lt;T&gt;::begin() [with T = int]()'
  [..path..]/Trie.h:230: note: candidates are: Trie&lt;int&gt;::const_trie_iterator&amp; Trie&lt;int&gt;::const_trie_iterator::operator=(const Trie&lt;int&gt;::const_trie_iterator&amp;)
  </code></pre>
  <p>So, I believe the non-const version of <code>begin</code> is not used.</p>
  <p>I contemplated creating an <code>operator=(const Trie&lt;T&gt;::const_trie_iterator&amp;)</code> method for the non-const iterator but I don't see that in the <code>STD</code> lib and I would have to const_cast the iterator. What should I do?</p>