title: C++ type_traits template to add reference if not const
body: <p>I have a class template that accepts a type <code>T</code>. It has a method. I want this method to return type <code>T</code> if it is <code>const</code> and <code>T&amp;</code> if it is non-<code>const</code>.</p>
  
  <pre><code>template&lt;typename T&gt;
  class C
  {
      static typename add_reference_if_non_const&lt;T&gt;::type method();
  };
  
  int main()
  {
      assert( is_same&lt;result_of&lt;C&lt;int&gt;::method()&gt;::type, int&amp;&gt;::value );
      assert( is_same&lt;result_of&lt;C&lt;const int&gt;::method()&gt;::type, const int&gt;::value );
  }
  </code></pre>
  
  <p>How can I do it?</p>