title: Safely return reference to member variable
body: <p>In the library I am designing, I sometimes need read-access to large member variables of classes. Because of their size I don't want to make a getter that returns by copying the member. I don't want them to be modifiable from the outside, so I can't have them public or return references to them. So I thought I would use a "reader":</p>
  
  <pre><code>class TestClass
  {
  public:
      explicit TestClass(double d): d_(d){}
  
      const double&amp; readD() const { return d_; }
  
  private:
      double d_;
  };
  </code></pre>
  
  <p>(this is not really meant for doubles)</p>
  
  <p>But here somebody could const_cast the reference and access the data directly. Even without assuming malicious intent, somebody could safe a reference to the data-member and keep it around after the original object has gone out of scope. I know const references can keep a temporary viable, but that doesn't remove the const_cast-problem.
  So I came up with a workaround:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  template&lt;class T&gt;
  class SafeMemberReference
  {
  public:
      using type = T;
      SafeMemberReference(const T&amp; t) :t(t) {}
      explicit SafeMemberReference(T&amp;&amp; t) = delete;
  
      operator const T&amp; () &amp;&amp; {return t; }
      T get() &amp;&amp; {return t; }
  private:
      const T&amp; t;
  };
  
  class TestClass
  {
  public:
      explicit TestClass(double d): d_(d){}
  
      SafeMemberReference&lt;double&gt; readD() const { return d_; }
  
  private:
      double d_;
  };
  
  int main()
  {
      TestClass foo(1.2);
  
      // temporary from read can be used as temporary in expressions
      std::cout &lt;&lt; foo.readD() &lt;&lt; std::endl;
  
      // temporary can be used to copy from
      auto x = foo.readD().get();
  
      // lvalue can not be used, so a possible dangling reference is no problem
      auto ref = foo.readD();
      //std::cout &lt;&lt; ref &lt;&lt; std::endl;
   }
  </code></pre>
  
  <p>I have several questions to this:</p>
  
  <p>Q1) How necessary is this from an efficiency POV? the largest objects I am returning are dense complex matrices with dimensions of maybe 1000x1000. These copies may happen frequently</p>
  
  <p>Q2) Are my concerns about returning by const&amp; valid? </p>
  
  <p>Q3) Does this seem like a good solution? which drawbacks does it have?</p>