title: proxy class in template for subscript operator overloading
body: <p>I was brushing up on my C++ and wanted to implement a concept based on Scott Meyer's 'More Effective C++', Item 30. The thing is about distinguishing between reading and writing when using a subscript operator with a custom data structure.</p>
  
  <p>So what I'm experimenting with is to create a template used as follows:</p>
  
  <pre><code>map&lt;int&gt; im;
  im["foo"] = 69;
  cout &lt;&lt; im["foo"] &lt;&lt; endl; // outputs 69
  im["bar"] = im["foo"];
  </code></pre>
  
  <p>I came up with something like this, having a STL map inside the class for storing data and creating a Helper proxy class to handle dealing with subscript stuff.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  using std::string;
  
  namespace mymap {
  
  template &lt;typename T&gt; class map
  {
     private:
     std::map&lt;string,T&gt; container;
  
     public:
  
     class Helper {
        map&amp; m;
        string key;
        Helper (map&amp; mm, string kkey): m(mm), key(kkey) {};
  
        public:
  
        operator T() const // rvalue use
        {
           std::cout &lt;&lt; "op char()" &lt;&lt; std::endl;
           return m.container[key];
        }
  
        Helper&amp; operator=(T i) // lvalue use
        {
           m.container[key] = i;
           return *this;
        }
  
        Helper&amp; operator=(const Helper&amp; rhs) // lvalue use
        {
           m.container[key] = rhs.m.container[rhs.key];
           return *this;
        }
     };
  
  
     const Helper operator[]( string key) const // for const maps
     {
        return Helper(const_cast&lt;map&amp;&gt;(*this), key);
     }
  
     Helper operator[](string key) // for non-const maps
     {
        return Helper(*this, key);
     }
  
     friend class Helper;
  
  };
  
  } // mymap namespace
  </code></pre>
  
  <p>Although it builds for an instruction</p>
  
  <pre><code> using namespace mymap;
   using mymap::map;
   using std::cout;
  
   int main(void) 
   {
       map&lt;int&gt; im;
   }
  </code></pre>
  
  <p>in main(), but any other of the stuff I'm trying to accomplish breaks the build. Could you please tell me if there's something significant that I screwed up and how that can be fixed?</p>
  
  <p>It might be the templates structure, as I see in the first place:</p>
  
  <pre><code>error: ‘mymap::map&lt;T&gt;::Helper::Helper(mymap::map&lt;T&gt;&amp;, std::string) [with T = int, mymap::map&lt;T&gt; = mymap::map&lt;int&gt;, std::string = std::basic_string&lt;char&gt;]’ is private
  </code></pre>
  
  <p>I've seen a simmilar design concept with proxies at <a href="https://stackoverflow.com/questions/10788557/two-array-subscript-overloading-to-read-and-write">Two array subscript overloading to read and write</a>, however I'm willing to try the Meyer's approach in this one.</p>