{"id":102610,"date":"2019-06-24T07:00:00","date_gmt":"2019-06-24T14:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=102610"},"modified":"2019-06-24T06:37:04","modified_gmt":"2019-06-24T13:37:04","slug":"20190624-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190624-00\/?p=102610","title":{"rendered":"Detecting whether the <CODE>-opt<\/CODE> flag was passed to <CODE>cppwinrt.exe<\/CODE>: Using <CODE>__has_include<\/CODE>"},"content":{"rendered":"<p>I was upgrading the <a href=\"https:\/\/github.com\/Microsoft\/Windows-universal-samples\"> Window UWP Samples repo<\/a> to take advantage of the new <code>-opt<\/code> flag introduced in C++\/WinRT 2.0. This provides performance improvements for accessing static class members, and avoids having to register the type in your manifest for strictly in-module consumption.<\/p>\n<p>The new <code>-opt<\/code> flag enables these optimizations, but it also adds a new requirement: Your implementation file needs to <code>#include &lt;ClassName.g.cpp&gt;<\/code>. The problem is that I wanted to upgrade the samples one at a time, but that meant that the shared files needed to support both optimized and unoptimized builds, at least until I get them all converted.<\/p>\n<p>I was at a bit of a loss, because there was no obvious <code>#define<\/code> in <code>winrt\/base.h<\/code> that tells me whether the <code>-opt<\/code> flag was passed.<\/p>\n<p>And then I realized: I could use <code>__has_include<\/code>.<\/p>\n<p>C++17 introduced the <code>__has_include<\/code> preprocessor keyword which snoops around to determine whether a header file exists. The idea is that you could conditionalize based on whether an optional header file is present. For example, you might check for the presence of <code>xmmintrin.h<\/code> and conditionally enable SSE operations.<\/p>\n<p>In my case, I wouldn&#8217;t be probing for a system header file, but rather for a generated <code>.g.cpp<\/code> file produce by <code>cppwinrt.exe<\/code> in <code>-opt<\/code> mode.<\/p>\n<pre>#if __has_include(&lt;MainPage.g.cpp&gt;)\r\n#include &lt;MainPage.g.cpp&gt;\r\n#endif\r\n<\/pre>\n<p>If <code>cppwinrt.exe<\/code> were run with the <code>-opt<\/code> flag, then the <code>MainPage.g.cpp<\/code> file will exist in the <code>Generated Files<\/code> directory, and I can include it. If it were run without the <code>-opt<\/code> flag, then the <code>MainPage.g.cpp<\/code> file will not exist, and I skip over it.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looking in the cushions of the couch.<\/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-102610","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Looking in the cushions of the couch.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102610","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=102610"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102610\/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=102610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=102610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=102610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}