title: Template Return Type with Default Value
body: <p>So in writing a C++ template class, I have defined a method that returns an object of the templated type, as such:</p>
  
  <pre><code>template &lt;typename T&gt;
  class Foo
  {
  public:
      T GetFoo()
      {
          T value;
  
          //Do some stuff that might or might not set the value of 'value'
  
          return value;
      }
   };
  
  int main() {
  
      Foo&lt;int&gt; foo;
  
      foo.GetFoo();
  
      return 0;
  }
  </code></pre>
  
  <p>This gives the following warning:</p>
  
  <pre><code>prog.cpp: In member function ‘T Foo&lt;T&gt;::GetFoo() [with T = int]’:
  prog.cpp:15: warning: ‘value’ is used uninitialized in this function
  </code></pre>
  
  <p>I understand why this is happening - I am returning an uninitialized <code>int</code> as part of <code>GetFoo</code>.  The thing is, if I were to use <code>Foo&lt;SomeClass&gt;</code>, the line <code>T value;</code> would initialize <code>value</code> using the default constructor of <code>SomeClass</code>.</p>
  
  <p>I have managed to suppress this warning by doing the following:</p>
  
  <pre><code>    T GetFoo()
      {
          T value = T();
  
          //Do some stuff that might or might not set the value of 'value'
  
          return value;
      }
  </code></pre>
  
  <p>This seems to work for primitive types (such as <code>int</code> and <code>float</code>) and classes, at least so long as that class has a default constructor and copy constructor.  My question is - is this the accepted way of solving this problem?  Are there any side effects of this I should know about?</p>