title: C++ Difference between std::ref(T) and T&?
body: <p>I have some questions regarding this program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;type_traits&gt;
  #include &lt;functional&gt;
  using namespace std;
  template &lt;typename T&gt; void foo ( T x )
  {
      auto r=ref(x);
      cout&lt;&lt;boolalpha;
      cout&lt;&lt;is_same&lt;T&amp;,decltype(r)&gt;::value;
  }
  int main()
  {
      int x=5;
      foo (x);
      return 0;
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>false
  </code></pre>
  
  <p>I want to know, if <code>std::ref</code> doesn't return the reference of an object, then what does it do? Basically, what is the difference between:</p>
  
  <pre><code>T x;
  auto r = ref(x);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>T x;
  T &amp;y = x;
  </code></pre>
  
  <p>Also, I want to know why does this difference exist? Why do we need <code>std::ref</code> or <code>std::reference_wrapper</code> when we have references (i.e. <code>T&amp;</code>)?</p>