{"id":1480,"date":"2015-12-08T17:07:08","date_gmt":"2015-12-08T07:07:08","guid":{"rendered":"http:\/\/brnz.org\/hbr\/?p=1480"},"modified":"2016-03-08T08:30:47","modified_gmt":"2016-03-07T22:30:47","slug":"c14-and-volatile-implicity","status":"publish","type":"post","link":"https:\/\/brnz.org\/hbr\/?p=1480","title":{"rendered":"C++14 and volatile implicity"},"content":{"rendered":"<p>[Update 2016-03-07: It appears that this was a bug in VS2015, and <a href=\"https:\/\/msdn.microsoft.com\/en-US\/library\/mt656697(VS.140).aspx\">has been fixed in Update 2 RC<\/a>]<\/p>\n<p>In the process of upgrading Visual Studio 2012 to Visual Studio 2015, I encountered some brand new link errors that looked something like this:<\/p>\n<pre escaped=\"true\" lang=\"txt\" line=\"1\">error LNK2001: unresolved external symbol \r\n\"public: __cdecl FooData::FooData(struct FooData&amp; const &amp;)\"\r\n<\/pre>\n<p><span style=\"line-height: 1.6471;\">It&#8217;s not a new error in VS2015 &#8212; <\/span><a style=\"line-height: 1.6471;\" href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/f6xx1b1z(v=vs.110).aspx\">VS2012 can certainly produce it<\/a><span style=\"line-height: 1.6471;\">. I mean &#8220;new&#8221; in the sense that there were no problems linking this code when using the older compiler.<\/span><\/p>\n<p>The struct in question looks vaguely like this:<\/p>\n<pre escaped=\"true\" lang=\"cpp\" line=\"1\">struct FooData\r\n{\r\n  int m_Bar;\r\n  volatile int m_Baz;\r\n};<\/pre>\n<p>The problem is m_Baz. In C++14, the language\u00a0was changed to say that structs are not trivially constructible if they have non-static volatile members. And that, I think, is why there&#8217;s no default copy constructor being generated. I can&#8217;t\u00a0quote chapter and verse to back up that assertion, though.<\/p>\n<p>[Update: Actually&#8230; maybe not? I&#8217;m beginning to wonder if VS2015 is doing the wrong thing here.]<\/p>\n<p>But the fix is simple: add a copy constructor. And then, when the program fails to compile, declare a default constructor (because, of course, adding\u00a0a copy constructor causes the implicit default constructor to be marked as deleted).<\/p>\n<p>I found that developing an understanding of exactly what was happening and why to be the more difficult problem. Initially because the the compiler gave no indication that there was a problem at all, and willingly generated calls to a copy constructor that couldn&#8217;t possibly exist. Deeper than that, I&#8217;m still trying to piece together my own understanding of exactly why (and how) this change was made to the standard.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Update 2016-03-07: It appears that this was a bug in VS2015, and has been fixed in Update 2 RC] In the process of upgrading Visual Studio 2012 to Visual Studio 2015, I encountered some brand new link errors that looked something like this: error LNK2001: unresolved external symbol &#8220;public: __cdecl FooData::FooData(struct FooData&amp; const &amp;)&#8221; It&#8217;s &hellip; <a href=\"https:\/\/brnz.org\/hbr\/?p=1480\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;C++14 and volatile implicity&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,26],"tags":[],"_links":{"self":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1480"}],"collection":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1480"}],"version-history":[{"count":6,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1480\/revisions"}],"predecessor-version":[{"id":1511,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1480\/revisions\/1511"}],"wp:attachment":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}