{"id":102529,"date":"2019-05-30T07:00:00","date_gmt":"2019-05-30T14:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=102529"},"modified":"2019-05-30T06:13:40","modified_gmt":"2019-05-30T13:13:40","slug":"20190530-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190530-00\/?p=102529","title":{"rendered":"Why does my C++\/WinRT project get errors of the form &#8220;consume_Something: function that returns &#8216;auto&#8217; cannot be used before it is defined&#8221;?"},"content":{"rendered":"<p>Last time, we <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190529-00\/?p=102527\"> investigated a mysterious error that occurs when linking a C++\/WinRT project<\/a>, and I noted that there&#8217;s some good news and some bad news. The good news is that this error message is going away. The bad news is that it&#8217;s being replaced with a different error message that you have to learn.<\/p>\n<p>Let&#8217;s take another look at the code that triggers this error.<\/p>\n<pre>#include &lt;winrt\/Windows.Gaming.Input.h&gt;\r\n\r\nvoid CheckGamepads()\r\n{\r\n    auto gamepads =\r\n        winrt::Windows::Gaming::Input::Gamepad::Gamepads();\r\n    for (auto&amp;&amp; gamepad : gamepads)\r\n    {\r\n        check(gamepad);\r\n    }\r\n}\r\n<\/pre>\n<p>Instead of getting a linker error, you get a compile-time error at the point you attempt to consume an interface whose header file you failed to include.<\/p>\n<pre style=\"white-space: pre-wrap;\">test.cpp(7): error C3779: winrt::<wbr \/>impl::<wbr \/>consume_<wbr \/>Windows_<wbr \/>Foundation_<wbr \/>Collections_<wbr \/>IIterable&lt;D,<wbr \/>winrt::<wbr \/>Windows::<wbr \/>Gaming::<wbr \/>Input::<wbr \/>Gamepad&gt;::<wbr \/>First': a function that returns 'auto' cannot be used <!--\r\n-->before it is defined\r\nwith\r\n[\r\n    D=winrt::<wbr \/>:Windows::<wbr \/>:Gaming::<wbr \/>:Input::<wbr \/>:Gamepad\r\n]\r\nnote: see declaration of 'winrt::<wbr \/>impl::<wbr \/>consume_<wbr \/>Windows_<wbr \/>Foundation_<wbr \/>Collections_<wbr \/>IIterable&lt;D,winrt::<wbr \/>Windows::<wbr \/>Gaming::<wbr \/>Input::<wbr \/>Gamepad&gt;::<wbr \/>First'\r\nwith\r\n[\r\n    D=winrt::<wbr \/>Windows::<wbr \/>Gaming::<wbr \/>Input::<wbr \/>IVisualCollection\r\n]\r\n<\/pre>\n<p><b>For the impatient<\/b>: The problem is that you are missing the header file for the interface you are using. In this case, we are using <code>Windows.<\/code><code>Foundation.<\/code><code>Collections.<\/code><code>IIterable<\/code>, so we need to include<\/p>\n<pre>#include &lt;winrt\/Windows.Foundation.Collections.h&gt;\r\n<\/pre>\n<p>You can <a href=\"https:\/\/github.com\/microsoft\/xlang\/pull\/427\"> read the pull request that makes the change to detect the error at compile time<\/a> rather than link time.<\/p>\n<p>The trick is that the forward-declared methods are declared as returning <code>auto<\/code> with no trailing return type and no body. This means &#8220;I want the compiler to deduce the return type (but I&#8217;m not giving any clues yet).&#8221; If you try to call the method before the method has been implemented, then the compiler reports an error because it doesn&#8217;t yet have the necessary information to determine the return type.<\/p>\n<p>Hopefully the new error message will make it easier to figure out what went wrong. At least it gives you a file name and line number that points to the place where the unimplemented method is used, and the error mesage includes the name of the type whose definition is missing.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Narrowing down the source of the missing header file.<\/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-102529","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Narrowing down the source of the missing header file.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102529","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=102529"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102529\/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=102529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=102529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=102529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}