{"id":94836,"date":"2016-12-01T07:00:00","date_gmt":"2016-12-01T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=94836"},"modified":"2019-03-13T10:34:26","modified_gmt":"2019-03-13T17:34:26","slug":"20161201-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20161201-00\/?p=94836","title":{"rendered":"What is __wchar_t (with the leading double underscores) and why am I getting errors about it?"},"content":{"rendered":"<p>The Microsoft Visual C++ compiler has a compiler option called <code>\/Zc:wchar_t<\/code> which lets you control what the symbol <code>wchar_t<\/code> means. <\/p>\n<p>According to the C++ standard, <code>wchar_t<\/code> is a distinct native type, and that&#8217;s what the Visual C++ compiler defaults to. However, you can set <code>\/Zc:wchar_t-<\/code>, and that suppresses the intrinsic definition of <code>wchar_t<\/code>, allowing you to define it to whatever you like. And for Windows, this historically means <\/p>\n<pre>\ntypedef unsigned short wchar_t;\n<\/pre>\n<p>because Windows predates the versions of the C and C++ standards that introduced <code>wchar_t<\/code> as a native type. <\/p>\n<p>So now you have a problem if you are writing a library that will be consumed both by old-school code written with <code>wchar_t<\/code> defined as an alias for <code>unsigned short<\/code> and by new-school code written with <code>wchar_t<\/code> as an intrinsic type. What data type do you use for your string parameters? <\/p>\n<p>Well, if your library uses C linkage, then you&#8217;re in luck. Since the intrinsic <code>wchar_t<\/code> is a 16-bit unsigned integer in Visual C++, it is binary-compatible with <code>unsigned short<\/code>, so you can declare your function as accepting <code>wchar_t<\/code> in the header file, and each client will interpret it through their own <code>wchar_t<\/code>-colored glasses: Code that is wearing the <code>\/Zc:wchar_t<\/code> glasses will see the native <code>wchar_t<\/code> Type. Code that is wearing the <code>\/Zc:wchar_t-<\/code> glasses will see an <code>unsigned short<\/code>. And since C linkage is not decorated, you can export one function that accepts a <code>wchar_t<\/code>, and it will interoperate with either definition. <\/p>\n<p>That works for undecorated functions, but what about languages like C++ that use decoration to encode the types of the parameters? Which decoration do you use? <\/p>\n<p><a HREF=\"http:\/\/dilbert.com\/strip\/1996-01-27\">Let&#8217;s do both<\/a>. <\/p>\n<p>What you do is write two versions of your function, one that takes an <code>unsigned short<\/code> and one that takes a <code>__wchar_t<\/code>. That double-underscore version represents &#8220;The native type for <code>wchar_t<\/code> that is used by <code>\/Zc:wchar_t<\/code>.&#8221; <\/p>\n<p>In other words, <code>\/Zc:wchar_t<\/code> results in the compiler internally doing the equivalent of <\/p>\n<pre>\ntypedef __wchar_t wchar_t;\n<\/pre>\n<p>It makes the symbol <code>wchar_t<\/code> an alias for the internal <code>__wchar_t<\/code> type. <\/p>\n<p>So let&#8217;s say you have a function called <code>DoSomething<\/code> that takes a wide string, and you want to accept clients compiled with either setting for <code>\/Zc:wchar_t<\/code>. <\/p>\n<pre>\n\/\/ Something.h\n\nbool DoSomething(const __wchar_t* s);\nbool DoSomething(const unsigned short* s);\n<\/pre>\n<p>This declares two versions of the function. The first will be matched by code compiled with <code>\/Zc:wchar_t<\/code>. The second will be matched by code compiled with <code>\/Zc:wchar_t-<\/code>. <\/p>\n<p>Your implementation goes like this: <\/p>\n<pre>\n\/\/ Something.cpp\n#include &lt;Something.h&gt;\n\nbool DoSomethingWorker(const wchar_t* s)\n{\n ... implementation ...\n}\n\nbool DoSomething(const __wchar_t* s)\n{\n    return DoSomethingWorker(reinterpret_cast&lt;const wchar_t*&gt;(s));\n}\n\nbool DoSomething(const unsigned short* s)\n{\n    return DoSomethingWorker(reinterpret_cast&lt;const wchar_t*&gt;(s));\n}\n<\/pre>\n<p>As noted earlier, callers who compile with <code>\/Zc:wchar_t<\/code> will match the first version of <code>Do&shy;Something<\/code>; callers who compile with <code>\/Zc:wchar_t-<\/code> will match the second. But both of them funnel into a common implementation, which we declare with <code>wchar_t<\/code>, so that it matches the <code>\/Zc:wchar_t<\/code> setting used by the library itself. <\/p>\n<p>Okay, so to answer the opening question: <code>__wchar_t<\/code> is the name for the intrinsic data type for wide strings. If you compile with <code>\/Zc:wchar_t<\/code>, then that&#8217;s the data type that <code>wchar_t<\/code> maps to. The funny name exists so that code compiled with <code>\/Zc:wchar_t-<\/code> can access it too, and so that code which wants to be <code>\/Zc:wchar_t<\/code>-agnostic can explicitly refer to the internal native type. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s the internal wchar_t.<\/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-94836","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It&#8217;s the internal wchar_t.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94836","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=94836"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94836\/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=94836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=94836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=94836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}