{"id":111140,"date":"2025-05-02T07:00:00","date_gmt":"2025-05-02T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=111140"},"modified":"2025-05-02T06:08:48","modified_gmt":"2025-05-02T13:08:48","slug":"20250502-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20250502-00\/?p=111140","title":{"rendered":"Using type aliasing to avoid the ODR problem with conditional compilation, part 2"},"content":{"rendered":"<p>Last time, we tried <a title=\"Using type aliasing to avoid the ODR problem with conditional compilation, part 1\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20250501-00\/?p=111134\"> using type aliasing to avoid the ODR problem with conditional compilation<\/a>, but we ran into trouble with cases where a change in type does not participate in overload resolution.<\/p>\n<p>But we can still salvage the situation.<\/p>\n<p>We can follow the same basic pattern, but instead of allowing the debug and non-debug versions to coexist, we can simply prohibit them from coexisting.<\/p>\n<p>First, let&#8217;s isolate the two versions into separate translation units.<\/p>\n<pre>\/\/ widgetimpl.h\r\n#include \"widget.h\"\r\n\r\ntemplate&lt;bool debug&gt;\r\nvoid Widget&lt;debug&gt;::Widget()\r\n{\r\n    \u27e6 constructor stuff \u27e7\r\n}\r\n\r\ntemplate&lt;bool debug&gt;\r\nvoid Widget&lt;debug&gt;::SetName(std::string const&amp; name)\r\n{\r\n    m_name = name;\r\n    Log(\"Name changed\");\r\n    Log(name);\r\n}\r\n\r\n\/\/ widgetdebug.cpp\r\n#include \"widgetimpl.h\"\r\n\r\ntemplate struct WidgetT&lt;true&gt;;\r\n\r\n\/\/ widgetnondebug.cpp\r\n#include \"widgetimpl.h\"\r\n\r\ntemplate struct WidgetT&lt;false&gt;;\r\n<\/pre>\n<p>Now we can use the One Definition Rule to our advantage: Declare the same variable in each translation unit.<\/p>\n<pre>\/\/ widgetdebug.cpp\r\n#include \"widgetimpl.h\"\r\n\r\ntemplate struct WidgetT&lt;true&gt;;\r\n\r\nnamespace Widgets::internal\r\n{\r\n    extern const bool is_debug = true;\r\n}\r\n\r\n\/\/ widgetnondebug.cpp\r\n#include \"widgetimpl.h\"\r\n\r\ntemplate struct WidgetT&lt;false&gt;;\r\n\r\nnamespace Widgets::internal\r\n{\r\n    extern const bool is_debug = false;\r\n}\r\n<\/pre>\n<p>If somebody uses a debugging-mode Widget, then that pulls in the explicit instantiation from <tt>widgetdebug.obj<\/tt>, and that in turn pulls in the debugging definition of <code>Widgets::<wbr \/>internal::<wbr \/>is_debug<\/code>. And if somebody else uses a non-debugging Widget, then that will pull in the explicit instantiation from <tt>widgetnondebug.obj<\/tt>, which in turn pulls in the non-debugging definition of <code>Widgets::<wbr \/>internal::<wbr \/>is_debug<\/code>.<\/p>\n<p>Two definitions for the same thing is a diagnosable One Definition Rule violation, and the linker will complain.<\/p>\n<p>Phew, we did it.<\/p>\n<p>Now, if you are using the Microsoft Visual C++ compiler, <a title=\"Using #pragma detect_mismatch to help catch ODR violations\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20160803-00\/?p=94015\"> you could have used <code>#pragma detect_mismatch<\/code> from the outset<\/a> and avoided this hassle.<\/p>\n<pre>\/\/ widget.h\r\n\r\nstruct Widget\r\n{\r\n    Widget();\r\n\r\n    void SetName(std::string const&amp; name);\r\n\r\n    \u27e6 more stuff \u27e7\r\n\r\n#ifdef EXTRA_WIDGET_DEBUGGING\r\n    Logger m_logger;\r\n\r\n    void Log(std::string const&amp; message) {\r\n        m_logger.log(message);\r\n    }\r\n#else\r\n    void Log(std::string const&amp; message) {\r\n        \/\/ no extra logging\r\n    }\r\n#endif\r\n\r\n    std::string m_name;\r\n};\r\n\r\n#ifdef EXTRA_WIDGET_DEBUGGING\r\n#pragma detect_mismatch(\"widget build mode\", \"debug\")\r\n#else\r\n#pragma detect_mismatch(\"widget build mode\", \"nondebug\")\r\n#endif\r\n<\/pre>\n<p>If two clients set <code>EXTRA_<wbr \/>WIDGET_<wbr \/>DEBUGGING<\/code> differently, the linker will complain about the mismatch.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ensuring that you pick a side.<\/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-111140","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Ensuring that you pick a side.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111140","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=111140"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111140\/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=111140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=111140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=111140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}