title: Can I avoid explicitly writing out my class-template parameter-type during template instantiation?
body: <p>I wrote a little templated RAII-class called <code>SaveGuard</code> whose constructor makes a copy of the current state of a specified object, and then later on the destructor restores the object from that saved state.  That way I can make temporary changes to an object and I am guaranteed that they will be auto-reverted at the end of the current scope (see code below).</p>
  <p>That all works fine; my question is this:  Is there a way for me to avoid having to explicitly type out the type of the to-be-saved object every time I declare a <code>SaveGuard</code>?  i.e. rather than typing this:</p>
  <pre><code>const SaveGuard&lt;std::string&gt; guard(myStr);
  </code></pre>
  <p>I'd prefer to type something like this:</p>
  <pre><code>const SaveGuard&lt;&gt; guard(myStr);
  </code></pre>
  <p>Since the type of the object might be rather elaborate, and I might be declaring <code>SaveGuard</code> objects for it in many places, it would save a lot of typing and declutter my code if I could.</p>
  <p>However, trying that yields this error:</p>
  <pre><code>temp.cpp:23:17: error: too few template arguments for class template 'SaveGuard'
        const SaveGuard&lt;&gt; guard(myStr);
  </code></pre>
  <p>Code follows:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  /** Convenience class to place on the stack for RAII-swapping of a table out to temporary storage and then back again in the destructor */
  template&lt;class T&gt; class SaveGuard
  {
  public:
     SaveGuard(T &amp; saveMe) : _saveMe(saveMe), _tempHolder(saveMe) {/* empty */}  // save the current value
     ~SaveGuard() {_saveMe = _tempHolder;}  // restore the saved value
  
  private:
     T &amp; _saveMe;
     T _tempHolder;
  };
  
  int main(int argc, char ** argv)
  {
     std::string myStr = &quot;foo&quot;;
  
     std::cout &lt;&lt; &quot;At point A, myStr=&quot; &lt;&lt; myStr &lt;&lt; std::endl;
  
     {
        const SaveGuard&lt;std::string&gt; guard(myStr);
  
        // Make some temporary modifications to myStr
        myStr += &quot;bar&quot;;
        std::cout &lt;&lt; &quot;At point B, myStr=&quot; &lt;&lt; myStr &lt;&lt; std::endl;
     }
  
     std::cout &lt;&lt; &quot;At point C, myStr=&quot; &lt;&lt; myStr &lt;&lt; std::endl;
     return 0;
  }
  </code></pre>
  <p>When run, the code print out this:</p>
  <pre><code>At point A, myStr=foo
  At point B, myStr=foobar
  At point C, myStr=foo
  </code></pre>