title: What is the most idiomatic way to "delay" the construction of a C++ object?
body: <p>For built-in types, like int, you can delay the initialization simply writing nothing. Is there a way to do the same for C++ objects?</p>
  
  <p>I wrote this code that does the job, but I was wondering if there is an idiomatic way. If so, what is it? Was it even possible before the introduction of aligned storage?</p>
  
  <pre><code>#include &lt;utility&gt;
  #include &lt;type_traits&gt;
  
  template &lt;typename T&gt;
  struct delayed {
      delayed() { unset_init(); }
      template &lt;typename...Args&gt; void init(Args&amp;&amp;... args) {
          new ( memory() ) T(std::forward&lt;Args&gt;(args)...);
          set_init();
      }
  
      operator T*() {
          return memory();
      }
  
      ~delayed() {
          if (get_init()) {
              memory()-&gt;~T();
              unset_init();
          }
      }
  
  private:
      T* memory() { return reinterpret_cast&lt;T*&gt;(&amp;bytes_); }
      unsigned char* raw_memory() { return reinterpret_cast&lt;unsigned char*&gt;(&amp;bytes_); }
  
      unsigned char&amp; init() { return *( raw_memory() + sizeof(T) ); }
      bool get_init() { return init() != 0; }
      void set_init() { init() = 1; }
      void unset_init() { init() = 0; }
  
      typename std::aligned_storage&lt;sizeof(T) + 1, alignof(T)&gt;::type bytes_{};
  };
  </code></pre>