title: Async constructor in C++11
body: <p>Sometimes I need to create objects whose constructors take very long time to execute.
  This leads to responsiveness problems in UI applications.</p>
  
  <p>So I was wondering if it could be sensible to write a constructor designed to be called asynchronously, by passing a callback to it which will alert me when the object is available.</p>
  
  <p>Below is a sample code:</p>
  
  <pre><code>class C
  {
  public:
      // Standard ctor
      C()
      {
          init();
      }
  
      // Designed for async ctor
      C(std::function&lt;void(void)&gt; callback)
      {
          init();
          callback();
      }
  
  private:
      void init() // Should be replaced by delegating costructor (not yet supported by my compiler)
      {
          std::chrono::seconds s(2);
          std::this_thread::sleep_for(s);
          std::cout &lt;&lt; "Object created" &lt;&lt; std::endl;
      }
  };
  
  int main(int argc, char* argv[])
  {
      auto msgQueue = std::queue&lt;char&gt;();
      std::mutex m;
      std::condition_variable cv;
      auto notified = false;
  
      // Some parallel task
      auto f = []()
      {
          return 42;
      };
  
      // Callback to be called when the ctor ends
      auto callback = [&amp;m,&amp;cv,&amp;notified,&amp;msgQueue]()
      {
          std::cout &lt;&lt; "The object you were waiting for is now available" &lt;&lt; std::endl;
          // Notify that the ctor has ended
          std::unique_lock&lt;std::mutex&gt; _(m);
          msgQueue.push('x');
          notified = true;
          cv.notify_one();
      };
  
      // Start first task
      auto ans = std::async(std::launch::async, f);
  
      // Start second task (ctor)
      std::async(std::launch::async, [&amp;callback](){ auto c = C(callback); });
  
      std::cout &lt;&lt; "The answer is " &lt;&lt; ans.get() &lt;&lt; std::endl;
  
      // Mimic typical UI message queue
      auto done = false;
      while(!done)
      {
          std::unique_lock&lt;std::mutex&gt; lock(m);
          while(!notified)
          {
              cv.wait(lock);
          }
          while(!msgQueue.empty())
          {
              auto msg = msgQueue.front();
              msgQueue.pop();
  
              if(msg == 'x')
              {
                  done = true;
              }
          }
      }
  
      std::cout &lt;&lt; "Press a key to exit..." &lt;&lt; std::endl;
      getchar();
  
      return 0;
  }
  </code></pre>
  
  <p>Do you see any drawback in this design? Or do you know if there is a better approach?</p>
  
  <p><b>EDIT</b></p>
  
  <p>Following the hints of JoergB's answer, I tried to write a factory which will bear the responsibility to create an object in a sync or async way:</p>
  
  <pre><code>template &lt;typename T, typename... Args&gt;
  class FutureFactory
  {
  public:
      typedef std::unique_ptr&lt;T&gt; pT;
      typedef std::future&lt;pT&gt; future_pT;
      typedef std::function&lt;void(pT)&gt; callback_pT;
  
  public:
      static pT create_sync(Args... params)
      {
          return pT(new T(params...));
      }
  
      static future_pT create_async_byFuture(Args... params)
      {
          return std::async(std::launch::async, &amp;FutureFactory&lt;T, Args...&gt;::create_sync, params...);
      }
  
      static void create_async_byCallback(callback_pT cb, Args... params)
      {
          std::async(std::launch::async, &amp;FutureFactory&lt;T, Args...&gt;::manage_async_byCallback, cb, params...);
      }
  
  private:
      FutureFactory(){}
  
      static void manage_async_byCallback(callback_pT cb, Args... params)
      {
          auto ptr = FutureFactory&lt;T, Args...&gt;::create_sync(params...);
          cb(std::move(ptr));
      }
  };
  </code></pre>