title: How to change wchar.h to make wchar_t the same type as wint_t?
body: <p><strong>wchar_t</strong> is defined in <code>wchar.h</code></p>
  
  <p>Currently, if the developers want to use only <code>wchar_t</code>, they can not do
  this without getting type conversion warnings from the compiler. If
  <code>wchar_t</code> will be made the same type as <code>wint_t</code>, it will be good for both parties.
  The developers who want to have both <code>wint_t</code> and <code>wchar_t</code> in their
  programs (for example if they want their code to be compiled not only under
  glibc) can do this without getting compiler warnings. The developers who
  want to use only <code>wchar_t</code> (to avoid unnecessary hassle with using <code>wint_t</code> and
  explicit typecasting) can also do this without getting compiler warnings.
  And it will not bring any incompatibility or portability problems, except that if code using only <code>wchar_t</code> will be compiled on the machine which uses original <code>wchar.h</code>, the compiler will print those pesky warnings (if <code>-Wconversion</code> is enabled), but the compiled program will work absolutely the same way.</p>
  
  <p>The C standard (9899:201x 7.29) mentions:</p>
  
  <blockquote>
    <p>wchar_t and wint_t can be the same integer type.</p>
  </blockquote>
  
  <p>Also, in glibc wide characters are always
  ISO10646/Unicode/UCS-4, so they always use 4 bytes. Thus, nothing
  prevents making <code>wchar_t</code> the same type as <code>wint_t</code> in glibc.</p>
  
  <p>But it seems that developers of glibc do not want to make <code>wint_t</code> and
  <code>wchar_t</code> the same type for some reason. As such, I want to change the local copy of
  <code>wchar.h</code>.</p>
  
  <p>ISO10646/Unicode/UCS-4 uses <code>2^31</code> values for the extended character set
  (MSB being unused):</p>
  
  <pre><code>0xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
  </code></pre>
  
  <p>Notice, that a 4-byte type can hold <code>2^31</code> extra values (MSB being "1"):</p>
  
  <pre><code>1xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
  </code></pre>
  
  <p>Any of those extra values can be used to represent <code>WEOF</code>, thus one
  4-byte type can be used to hold all the character set <em>and</em> <code>WEOF</code>.</p>
  
  <p>Notice, that no recompilation of glibc is necessary to use the modified <code>wchar.h</code>, because
  <code>wint_t</code> can be signed or unsigned (since both <code>-1</code> and <code>0xffffffff</code> have MSB
  "1", in any representation, and since MSB is not used in
  ISO10646/Unicode/UCS-4).</p>
  
  <p>Definition of <code>wchar_t</code> is done somewhere in the following excerpt from <code>wchar.h</code>.
  How to change it to make <code>wchar_t</code> the same type as <code>wint_t</code>?</p>
  
  <pre><code>#ifndef _WCHAR_H
  
  #if !defined __need_mbstate_t &amp;&amp; !defined __need_wint_t
  # define _WCHAR_H 1
  # include &lt;features.h&gt;
  #endif
  
  #ifdef _WCHAR_H
  /* Get FILE definition.  */
  # define __need___FILE
  # if defined __USE_UNIX98 || defined __USE_XOPEN2K
  #  define __need_FILE
  # endif
  # include &lt;stdio.h&gt;
  /* Get va_list definition.  */
  # define __need___va_list
  # include &lt;stdarg.h&gt;
  
  # include &lt;bits/wchar.h&gt;
  
  /* Get size_t, wchar_t, wint_t and NULL from &lt;stddef.h&gt;.  */
  # define __need_size_t
  # define __need_wchar_t
  # define __need_NULL
  #endif
  #if defined _WCHAR_H || defined __need_wint_t || !defined __WINT_TYPE__
  # undef __need_wint_t
  # define __need_wint_t
  # include &lt;stddef.h&gt;
  
  /* We try to get wint_t from &lt;stddef.h&gt;, but not all GCC versions define it
     there.  So define it ourselves if it remains undefined.  */
  # ifndef _WINT_T
  /* Integral type unchanged by default argument promotions that can
     hold any value corresponding to members of the extended character
     set, as well as at least one value that does not correspond to any
     member of the extended character set.  */
  #  define _WINT_T
  typedef unsigned int wint_t;
  # else
  /* Work around problems with the &lt;stddef.h&gt; file which doesn't put
     wint_t in the std namespace.  */
  #  if defined __cplusplus &amp;&amp; defined _GLIBCPP_USE_NAMESPACES \
        &amp;&amp; defined __WINT_TYPE__
  __BEGIN_NAMESPACE_STD
  typedef __WINT_TYPE__ wint_t;
  __END_NAMESPACE_STD
  #  endif
  # endif
  
  /* Tell the caller that we provide correct C++ prototypes.  */
  # if defined __cplusplus &amp;&amp; __GNUC_PREREQ (4, 4)
  #  define __CORRECT_ISO_CPP_WCHAR_H_PROTO
  # endif
  #endif
  
  #if (defined _WCHAR_H || defined __need_mbstate_t) &amp;&amp; !defined ____mbstate_t_defined
  # define ____mbstate_t_defined  1
  /* Conversion state information.  */
  typedef struct
  {
    int __count;
    union
    {
  # ifdef __WINT_TYPE__
      __WINT_TYPE__ __wch;
  # else
      wint_t __wch;
  # endif
      char __wchb[4];
    } __value;        /* Value so far.  */
  } __mbstate_t;
  #endif
  #undef __need_mbstate_t
  
  
  /* The rest of the file is only used if used if __need_mbstate_t is not
     defined.  */
  #ifdef _WCHAR_H
  
  # ifndef __mbstate_t_defined
  __BEGIN_NAMESPACE_C99
  /* Public type.  */
  typedef __mbstate_t mbstate_t;
  __END_NAMESPACE_C99
  #  define __mbstate_t_defined 1
  # endif
  
  #ifdef __USE_GNU
  __USING_NAMESPACE_C99(mbstate_t)
  #endif
  
  #ifndef WCHAR_MIN
  /* These constants might also be defined in &lt;inttypes.h&gt;.  */
  # define WCHAR_MIN __WCHAR_MIN
  # define WCHAR_MAX __WCHAR_MAX
  #endif
  
  #ifndef WEOF
  # define WEOF (0xffffffffu)
  #endif
  
  /* For XPG4 compliance we have to define the stuff from &lt;wctype.h&gt; here
     as well.  */
  #if defined __USE_XOPEN &amp;&amp; !defined __USE_UNIX98
  # include &lt;wctype.h&gt;
  #endif
  
  
  __BEGIN_DECLS
  
  __BEGIN_NAMESPACE_STD
  /* This incomplete type is defined in &lt;time.h&gt; but needed here because
     of `wcsftime'.  */
  struct tm;
  __END_NAMESPACE_STD
  /* XXX We have to clean this up at some point.  Since tm is in the std
     namespace but wcsftime is in __c99 the type wouldn't be found
     without inserting it in the global namespace.  */
  __USING_NAMESPACE_STD(tm)
  </code></pre>