Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How big is wchar_t with GCC?

Tags:

c++

gcc

macros

GCC supports -fshort-wchar that switches wchar_t from 4, to two bytes.

What is the best way to detect the size of wchar_t at compile time, so I can map it correctly to the appropriate utf-16 or utf-32 type? At least, until c++0x is released and gives us stable utf16_t and utf_32_t typedefs.

#if ?what_goes_here?
  typedef wchar_t Utf32;
  typedef unsigned short Utf16;
#else
  typedef wchar_t Utf16;
  typedef unsigned int Utf32;
#endif
like image 796
Chris Becke Avatar asked Feb 02 '11 10:02

Chris Becke


1 Answers

You can use the macros

__WCHAR_MAX__
__WCHAR_TYPE__

They are defined by gcc. You can check their value with echo "" | gcc -E - -dM

As the value of __WCHAR_TYPE__ can vary from int to short unsigned int or long int, the best for your test is IMHO to check if __WCHAR_MAX__ is above 2^16.

#if __WCHAR_MAX__ > 0x10000
  typedef ...
#endif
like image 144
Didier Trosset Avatar answered Sep 25 '22 00:09

Didier Trosset