title: CRTP and lifetime extension
body: <p>My question is how to make lifetime extension work with CRTP. For example, the following code is perfectly valid:</p>
  
  <pre><code>struct A {
      const int&amp; ref;
  };
  
  struct B {
      const A&amp; a;
  };
  
  int main() {
      B b{{123}};
      return b.a.ref;
  }
  </code></pre>
  
  <p>Its CRTPed version is not:</p>
  
  <pre><code>template &lt;class DerivedT&gt;
  class Gettable {
  public:
      int Get() const {
          return static_cast&lt;const DerivedT*&gt;(this)-&gt;GetImpl();
      }
  };
  
  class A : public Gettable&lt;A&gt; {
      friend class Gettable&lt;A&gt;;
  public:
      A(int r) : ref{r}{}
  
  private:
      int GetImpl() const {
          return ref;
      }
  
      const int&amp; ref;
  };
  
  template &lt;class T&gt;
  class B {
  public:
      B(const Gettable&lt;T&gt;&amp; gettable) : get_{gettable}{}
      int DifferentGet() const {
          return get_.Get();
      }
  
  private:
      const Gettable&lt;T&gt;&amp; get_;
  };
  
  int main() {
      B b{A{123}};
      return b.DifferentGet();
  }
  </code></pre>
  
  <p>The problem is that the original <code>A</code> and its <code>Gettable&lt;A&gt;</code> subobject only exist till the the of <code>B</code> constructor.</p>
  
  <p>I have two questions:</p>
  
  <p>1) Why? It is in no way different from the first case of structs, every lifetime is known at compile time, so I believe that compiler should be able to extend lifetime of all temporaries.</p>
  
  <p>2) Is there any good way to overcome this issue?</p>