title: const correctness and return values - C++
body: <p>Please consider the following code.</p>
  
  <pre><code>struct foo
  {
  };
  
  template&lt;typename T&gt;
  class test
  {
  public:   
  
      test() {} 
  
      const T&amp; value() const
      {
          return f;
      }
  
  private:
      T f;
  };
  
  
  int main()
  {
      const test&lt;foo*&gt; t;
      foo* f = t.value();
      return 0;
  }
  </code></pre>
  
  <p><code>t</code> is a <code>const</code> variable and <code>value()</code> is a constant member-function which returns <code>const T&amp;</code>. AFAIK, a <code>const</code> type is not assignable to a non-const type. But how <code>foo* f = t.value();</code> compiles well. How this is happening and how can I ensure <code>value()</code> can be only assigned to <code>const foo*</code>?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I found that, this is happening on when templates are used. Following code works as expected.</p>
  
  <pre><code>class test
  {
  public:   
  
      test() {} 
  
      const foo* value() const { return f; }
  
  private:
      foo* f;
  };
  
  
  int main()
  {
      const test t;
      foo* f = t.value(); // error here
      return 0;
  }
  </code></pre>
  
  <p>Why the problem is happening when templates are used?</p>