{"id":109195,"date":"2023-12-27T07:00:00","date_gmt":"2023-12-27T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109195"},"modified":"2023-12-27T08:21:23","modified_gmt":"2023-12-27T16:21:23","slug":"20231227-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20231227-00\/?p=109195","title":{"rendered":"What does it mean when the compiler says that it can&#8217;t convert something to itself?"},"content":{"rendered":"<p>A customer encountered a very strange error message from the Visual C++ compiler:<\/p>\n<pre style=\"white-space: pre-wrap;\">oops.cpp(7): Error C2664: 'void something(blah)' cannot convert argument 1 from 'blah' to 'blah'\r\n<\/pre>\n<p>Why is the compiler complaining that it cannot convert a <code>blah<\/code> to a <code>blah<\/code>? How is it not possible to convert something to itself?<\/p>\n<p>The answer is given in the next line of the error message:<\/p>\n<pre>oops.cpp(7): note: use of undefined type 'blah'\r\n<\/pre>\n<p>Here&#8217;s a sample program that demonstrates the problem.<\/p>\n<pre>struct blah;\r\n\r\nvoid something(blah);\r\n\r\nvoid test(blah&amp; b)\r\n{\r\n    something(b); \/\/ error here\r\n}\r\n<\/pre>\n<p>The problem is that the <code>test<\/code> function is passing a <code>blah<\/code> object by value to the <code>something<\/code> function. This requires the compiler to convert the thing you passed (a <code>blah<\/code> object) to the thing the function accepts (a <code>blah<\/code> object), which would normally be accomplished by using the copy constructor. But the compiler can&#8217;t find a copy constructor for <code>blah<\/code>, so it complains.<\/p>\n<p>Now, the reason it can&#8217;t find a copy constructor is that the type <code>blah<\/code> has never been defined. All that exists is a forward reference. That&#8217;s what the second error message is trying to tell you: &#8220;You said <code>blah<\/code>, but I don&#8217;t know anything about <code>blah<\/code>.&#8221;<\/p>\n<p><b>Bonus chatter<\/b>: Other compilers produce slightly less confusing error messages by complaining about the incompleteness first.<\/p>\n<pre style=\"white-space: pre-wrap;\">\/\/ gcc\r\noops.cpp:7:15: error: invalid use of incomplete type 'struct blah'\r\noops.cpp:1:8: note: forward declaration of 'struct blah'\r\n\r\n\/\/ clang\r\noops.cpp:11:15: error: argument type 'blah' is incomplete\r\noops.cpp:1:8: note: forward declaration of 'blah'\r\n\r\n\/\/ icc\r\noops.cpp(11): error: cannot convert to incomplete class \"blah\"\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Why is it even thinking about a conversion?<\/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-109195","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Why is it even thinking about a conversion?<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109195","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=109195"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109195\/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=109195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}