title: Accessing type members in C++
body: <p>Given an container such as a <code>vector&lt;int&gt;</code></p>
  
  <pre><code>#include &lt;vector&gt;
  using namespace std;
  vector&lt;int&gt; v{1, 2, 3};
  </code></pre>
  
  <p>Why does it seem to be quite difficult to access the public type members such as <code>iterator</code> and <code>const_iterator</code>? As I understand it, these names are part of the class (not the object) and must be accessed via <code>::</code> to specify the scope, but is there a reason to forbid <code>v.const_iterator</code> when <code>v</code> is known?
  Example:</p>
  
  <pre><code>int f(v.iterator it) {
      return *it;
  }
  // or
  int g(v::iterator it) {
      return *it;
  }
  </code></pre>
  
  <p>A workaround would be using <code>decltype</code> as in:</p>
  
  <pre><code>int h(decltype(v)::iterator it) {
      return *it;
  }
  </code></pre>
  
  <p>But this approach does not even work in classes, as the following fails:</p>
  
  <pre><code>class A
  {
  public:
      int h(decltype(x)::iterator it) {
          return *it;
      }
  private:
      vector&lt;int&gt; x;
  };
  </code></pre>
  
  <h2>Edit</h2>
  
  <p>Just a little sidenote.
  As pointed out, the meaning of <code>v.iterator</code> would depend on the type of <code>v</code> at the point of usage (compile time) ignoring runtime polymorphism. But the same is true for static class members.
  Example:</p>
  
  <pre><code>struct A
  {
      static const int x = 1;
  };
  struct B : public A
  {
      static const int x = 2;
  };
  void eval()
  {
      B b;
      A&amp; ar = b;
      b.x; // 2
      ar.x; // 1, even though ar refers to the same underlying object (by the base type)
  }
  </code></pre>