title: std::unique_ptr deleted function, initializer_list - driven allocation
body: <p>All, </p>
  
  <p>When I instantiate a widgets array using an initializer-list format, a naked pointer that points to a member-variable widget instance compiles but after change to std::unique_ptr&lt;> gcc gives a compilation error regarding a deleted function. </p>
  
  <p>$ uname -a </p>
  
  <p>Linux .. 3.5.0-21-generic #32-Ubuntu SMP Tue Dec 11 18:51:59 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux</p>
  
  <p>$ g++ --version</p>
  
  <p>g++ (Ubuntu/Linaro 4.7.2-5ubuntu1) 4.7.2</p>
  
  <p>This code gives the following compiler error:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;memory&gt;
  
  class Widget
  {
  public:
      Widget() {}
  };
  
  class W1 : public Widget
  {
  public:
      W1() {}
  };
  
  class W2 : public Widget
  {
  public:
      W2() {}
  };
  
  class WFactory
  {
  public:
      WFactory(const int i)   : _w(new W1()) {}
      WFactory(const char* s) : _w(new W2()) {}
  
      ~WFactory() { _w.reset(nullptr); }
      // ~WFactory() { delete _w; }  &lt;--- for naked ptr
  
  private:
      // NOTE: does not compile
      std::unique_ptr&lt;Widget&gt;  _w; 
      // NOTE: does compile
      // Widget* _w;
  };
  
  int main()
  {
      std::unique_ptr&lt;Widget&gt; a(new W1()); // &lt;--- compiles fine
  
      WFactory wf[] { 4, "msg" };          // &lt;--- compiler error using unique_ptr&lt;&gt;
  }
  </code></pre>
  
  <p>error:</p>
  
  <pre><code>$ g++ -o unique_ptr  -std=c++11 -Wall  unique_ptr.cpp 
  unique_ptr.cpp: In function ‘int main()’:
  unique_ptr.cpp:36:30: error: use of deleted function ‘WFactory::WFactory(const WFactory&amp;)’
  unique_ptr.cpp:22:7: note: ‘WFactory::WFactory(const WFactory&amp;)’ is implicitly deleted because the default definition would be ill-formed:
  unique_ptr.cpp:22:7: error: use of deleted function ‘std::unique_ptr&lt;_Tp, _Dp&gt;::unique_ptr(const std::unique_ptr&lt;_Tp, _Dp&gt;&amp;) [with _Tp = Widget; _Dp = std::default_delete&lt;Widget&gt;; std::unique_ptr&lt;_Tp, _Dp&gt; = std::unique_ptr&lt;Widget&gt;]’
  In file included from /usr/include/c++/4.7/memory:86:0,
               from unique_ptr.cpp:2:
  /usr/include/c++/4.7/bits/unique_ptr.h:262:7: error: declared here
  unique_ptr.cpp:36:30: error: use of deleted function ‘WFactory::WFactory(const WFactory&amp;)’
  unique_ptr.cpp:36:14: warning: unused variable ‘wf’ [-Wunused-variable]
  </code></pre>
  
  <p>I'm at a loss as to either: the mechanics behind the scenes that yields a deleted fcxn; or more simply, why the expressiveness of std::unique_ptr&lt;> appears to be restricted compared w/ a naked ptr.</p>
  
  <p>My question is:</p>
  
  <ul>
  <li>pilot error?</li>
  <li>compiler error? </li>
  <li>can I get my intended code to work w/ some change?</li>
  </ul>
  
  <p>Thank you.</p>
  
  <p><strong>Edit 1</strong></p>
  
  <p>Based on your answers, which I appreciate, I can make the following change to WFactory:</p>
  
  <p>(<strong>flagged as immoral code</strong>)</p>
  
  <pre><code>class WFactory
  {
  public:
      WFactory(const WFactory&amp; wf)
      {
          (const_cast&lt;WFactory&amp;&gt;(wf)).moveto(_w);
      }
  
      WFactory(const int i)   : _w(new W1()) {}
      WFactory(const char* s) : _w(new W2()) {}
  
      ~WFactory() { _w.reset(nullptr); }
  
      void moveto(std::unique_ptr&lt;Widget&gt;&amp; w)
      {
          w = std::move(_w);
      }
  private:
      std::unique_ptr&lt;Widget&gt;  _w; 
  };
  </code></pre>
  
  <p>and now the program compiles and runs. I appreciate that the standards folks wrote the specification for a reason, so I post my result as a good-faith specialization for my case at hand, where I really would like to emphasize the uniqueness of the ptr. </p>
  
  <p><strong>Edit 2</strong></p>
  
  <p>Based on Jonathan's replies, the following code does not suppress the implicit move ctor:</p>
  
  <pre><code>class WFactory
  {
  public:
      WFactory(const int i)   : _w(new W1()) {}
      WFactory(const char* s) : _w(new W2()) {}
  
  private:
      std::unique_ptr&lt;Widget&gt;  _w; 
  };
  </code></pre>
  
  <p>Note that there is no <code>~WFactory() {..}</code> at all. </p>
  
  <p>Perhaps there's ya-ans, but I have found that using a c++11-style iteration over wf[] in Main() brings back the no-copy-ctor-for-WFactory error. That is:</p>
  
  <pre><code>int Main()
  ..
      WFactory wf[] { 4, "msg" };
  
      for ( WFactory iwf : wf )    &lt;---- compiler error again
          // ..
  
      for (unsigned i = 0; i &lt; 2; ++i)  &lt;--- gcc happy
          wf[i] //  ..
  }
  </code></pre>
  
  <p>I guess it's self-evident that the new c++11-style iteration is doing an object copy. </p>