{"id":102932,"date":"2019-09-27T07:00:00","date_gmt":"2019-09-27T14:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=102932"},"modified":"2019-09-26T18:30:14","modified_gmt":"2019-09-27T01:30:14","slug":"20190927-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190927-00\/?p=102932\/","title":{"rendered":"Why does the compiler refuse to let me export my class? If I don&#8217;t export it, then the class works fine."},"content":{"rendered":"<p>Consider the following class:<\/p>\n<pre>class Node\r\n{\r\npublic:\r\n Node();\r\n \/\/ ... public methods ...\r\n\r\nprivate:\r\n std::vector&lt;std::unique_ptr&lt;Node&gt;&gt; children;\r\n};\r\n<\/pre>\n<p>This class works great as long as you don&#8217;t try to export it. Adding the <code>__declspec(dllexport)<\/code> declaration specifier causes the compiler to get mad.<\/p>\n<pre>class <span style=\"color: blue;\">__declspec(dllexport)<\/span> Node\r\n{\r\npublic:\r\n Node();\r\n \/\/ ... public methods ...\r\n\r\nprivate:\r\n std::vector&lt;std::unique_ptr&lt;Node&gt;&gt; children;\r\n};\r\n<\/pre>\n<p>The Visual C++ compiler now freaks out with<\/p>\n<pre style=\"white-space: pre-wrap;\">xutility(1784): error C2280: 'std::unique_ptr&lt;Node, std::default_delete&lt;_Ty&gt;&gt; &amp;std::unique_ptr&lt;_Ty, std::default_delete&lt;_Ty&gt;&gt;::<wbr \/>operator =(const std::unique_ptr&lt;_Ty, std::default_delete&lt;_Ty&gt;&gt; &amp;)': attempting to reference a deleted function\r\n        with\r\n        [\r\n            _Ty=Node\r\n        ]\r\nmemory(1969): note: see declaration of 'std::unique_ptr&lt;Node, std::default_delete&lt;_Ty&gt;&gt;::operator ='\r\n        with\r\n        [\r\n            _Ty=Node\r\n        ]\r\nmemory(1969): note: 'std::unique_ptr&lt;Node, std::default_delete&lt;_Ty&gt;&gt; &amp;std::unique_ptr&lt;_Ty, std::default_delete&lt;_Ty&gt;&gt;::<wbr \/>operator =(const std::unique_ptr&lt;_Ty, std::default_delete&lt;_Ty&gt;&gt; &amp;)': function was explicitly deleted\r\n        with\r\n        [\r\n            _Ty=Node\r\n        ]\r\n...\r\nvector(1044): note: while compiling class template member function 'void std::vector&lt;std::unique_ptr&lt;Node, std::default_delete&lt;_Ty&gt;&gt;, std::allocator&lt;std::unique_ptr&lt;_Ty, std::default_delete&lt;_Ty&gt;&gt;&gt;&gt;::<wbr \/>_Copy_assign(const std::vector&lt;std::unique_ptr&lt;_Ty, std::default_delete&lt;_Ty&gt;&gt;, std::allocator&lt;std::unique_ptr&lt;_Ty, std::default_delete&lt;_Ty&gt;&gt;&gt;&gt; &amp;, std::false_type)'\r\n        with\r\n        [\r\n            _Ty=Node\r\n        ]\r\nvector(1062): note: see reference to function template instantiation 'void std::vector&lt;std::unique_ptr&lt;Node, std::default_delete&lt;_Ty&gt;&gt;, std::allocator&lt;std::unique_ptr&lt;_Ty, std::default_delete&lt;_Ty&gt;&gt;&gt;&gt;::<wbr \/>_Copy_assign(const std::vector&lt;std::unique_ptr&lt;_Ty, std::default_delete&lt;_Ty&gt;&gt;, std::allocator&lt;std::unique_ptr&lt;_Ty, std::default_delete&lt;_Ty&gt;&gt;&gt;&gt; &amp;, std::false_type)' being compiled\r\n        with\r\n        [\r\n            _Ty=Node\r\n        ]\r\nnode.h(11): note: see reference to class template instantiation 'std::vector&lt;std::unique_ptr&lt;Node, std::default_delete&lt;_Ty&gt;&gt;, std::allocator&lt;std::unique_ptr&lt;_Ty, std::default_delete&lt;_Ty&gt;&gt;&gt;&gt;' being compiled\r\n        with\r\n        [\r\n            _Ty=Node\r\n        ]\r\n<\/pre>\n<p>Let&#8217;s simplify the error messages by taking out the default template parameters and the method return values, and performing the substitutions.<\/p>\n<pre style=\"white-space: pre-wrap;\">xutility(1784): error C2280: 'std::unique_ptr&lt;Node&gt;::<wbr \/>operator=(const std::unique_ptr&lt;Node&gt; &amp;)': attempting to reference a deleted function\r\nmemory(1969): note: see declaration of 'std::unique_ptr&lt;Node&gt;::operator='\r\nmemory(1969): note: 'std::unique_ptr&lt;Node&gt;::<wbr \/>operator=(const std::unique_ptr&lt;Node&gt; &amp;)': function was explicitly deleted\r\n...\r\nvector(1044): note: while compiling class template member function 'void std::vector&lt;std::unique_ptr&lt;Node&gt;&gt;::<wbr \/>_Copy_assign(const std::vector&lt;std::unique_ptr&lt;Node&gt;&gt; &amp;, std::false_type)'\r\nvector(1062): note: see reference to function template instantiation 'void std::vector&lt;std::unique_ptr&lt;Node&gt;&gt;::<wbr \/>_Copy_assign(const std::vector&lt;std::unique_ptr&lt;Node&gt;&gt; &amp;, std::false_type)' being compiled\r\nnode.h(11): note: see reference to class template instantiation 'std::vector&lt;std::unique_ptr&lt;Node&gt;&gt;' being compiled\r\n<\/pre>\n<p>What the compiler is trying to tell you is that it&#8217;s instantiating the <code>std::<\/code><code>vector<\/code> template with <code>std::<\/code><code>unique_ptr&lt;Node&gt;<\/code> as the type parameter, and it ran into trouble with the copy assignment operator. In order to copy the vector, it needs to copy each element, but <code>std::<\/code><code>unique_ptr&lt;Node&gt;<\/code> deleted its copy assignment operator.<\/p>\n<p>Okay, that makes sense. You can&#8217;t copy a vector of move-only objects. <!-- backref: Why does <CODE>std::is_copy_constructible<\/CODE> report that a vector of move-only objects is copy constructible? --> We saw that last time. But why does this problem occur only when you try to export the class? Shouldn&#8217;t it always be a problem?<\/p>\n<p>Template methods are instantiated on demand, and if you never try to copy the vector of move-only objects, then the copy assignment operator will never be invoked, and the compiler will never encounter the situation where it needs to copy a non-copyable object.<\/p>\n<p>That piano is perfectly safe, as long as you <a href=\"https:\/\/www.youtube.com\/watch?v=gUsJXwE73QU\"> don&#8217;t play B4<\/a>.<\/p>\n<p>Exporting a class forces the compiler to generate code for <i>every method<\/i>, just in case some other module tries to call it. In particular, the compiler must generate the <code>Node<\/code> copy constructor and copy assignment operator, but it can&#8217;t, and that&#8217;s why you&#8217;re getting the error.<\/p>\n<p>Exporting the class forces the compiler to play every note on the piano, just in case somebody ever presses it.<\/p>\n<p>To fix this problem, make your class explicitly non-copyable and non-copy-assignable.<\/p>\n<pre>class Node\r\n{\r\npublic:\r\n Node();\r\n\r\n <span style=\"color: blue;\">Node(Node const&amp;) = delete;\r\n Node&amp; operator=(Node const&amp;) = delete;<\/span>\r\n\r\n \/\/ ... public methods ...\r\nprivate:\r\n std::vector&lt;std::unique_ptr&lt;Node&gt;&gt; children;\r\n};\r\n<\/pre>\n<p>This removes the methods from the class, so the compiler won&#8217;t try to export them. Those methods didn&#8217;t work anyway, so removing them doesn&#8217;t cause any harm. It also makes the error messages much more comprehensible.<\/p>\n<pre>void f(const Node&amp; v)\r\n{\r\n  auto copy = v;\r\n}\r\n<\/pre>\n<p>Instead of some horrible 50-line error message, you get a very direct explanation of the problem:<\/p>\n<pre>error C2280: 'Node::Node(const Node &amp;)': attempting to reference a deleted function\r\nnote: see declaration of 'Node::Node'\r\nnote: 'Node::Node(const Node &amp;)': function was explicitly deleted\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You forced the compiler to try to compile it.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-102932","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You forced the compiler to try to compile it.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=102932"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102932\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=102932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=102932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=102932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}