title: CPP Template Parameter
body: <p>What's the consistency of the following codes:-</p>
  
  <p><strong>TMAP.h</strong></p>
  
  <pre><code>#include &lt;algorithm&gt;
  #include &lt;map&gt;
  
  template &lt;class K, class V&gt;
  class TMAP
  { 
  private:
      std::map &lt;K,V&gt;  map_K_V;
  
  public:
      bool key_exists(const K&amp; key) { return map_K_V.count( key ) &gt; 0; }
  
      bool insert(const K&amp; key, const V&amp; value)
      {
          if (!key_exists(key))
          {
              if (map_K_V.insert( std::make_pair( key, value ) ).second)
              {
                  return true;
              }
          }
          return false;
      }
  
      V get_value(const K&amp; key)
      {
          return map_K_V[ key ];
      }
  };
  </code></pre>
  
  <p>Template just like <em>std::map</em>, just more organized for other uses.</p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "TMAP.h"
  
  class A;
  
  TMAP&lt; std::string, A* &gt; map_cntr;
  
  class A
  {
    public:
      A( std::string nm )
      {
        name = nm;
        std::cout &lt;&lt; "A: " &lt;&lt; name &lt;&lt; ", object created." &lt;&lt; std::endl;
      }
  
      ~A()
      {
        std::cout &lt;&lt; "A: " &lt;&lt; name &lt;&lt; ", object destroyed." &lt;&lt; std::endl;
      }
  
      void printName()
      {
        std::cout &lt;&lt; "A: printName - " &lt;&lt; name &lt;&lt; std::endl;
      }
  
      void setName( std::string nm )
      {
          name = nm;
      }
  
    private:
      std::string name;
  };
  
  int main() {
      // Setting
      A* obj1 = new A( "obj1" );
      map_cntr.insert( "obj1", obj1 );
      obj1-&gt;printName();
  
      A* obj2 = new A( "obj2" );
      map_cntr.insert( "obj2", obj2 );
      obj2-&gt;printName();
  
      // Getting
      A* obj1_cpy;
      std::string obj1_name = "obj1";
  
      if (map_cntr.key_exists(obj1_name))
      {
          obj1_cpy = map_cntr.get_value(obj1_name);
          obj1_cpy-&gt;printName();
          obj1_cpy-&gt;setName("OBJ1");
          obj1_cpy-&gt;printName();
      }
  }
  </code></pre>
  
  <p>Outputs:  </p>
  
  <pre><code>A: obj1, object created.  
  A: printName - obj1  
  A: obj2, object created.  
  A: printName - obj2  
  A: printName - obj1  
  A: printName - OBJ1  
  </code></pre>
  
  <p>Outputs are as expected. Besides I've heard somewhere that using <em>std::string</em> as template parameter is not ideal just like in the above case somewhat relating to memory or pointer. Is it fair?</p>