我认为,检查是否支持这种类型是一个依赖于平台的事情。例如,GCC 定义:__CHAR16_TYPE__
and __CHAR32_TYPE__
如果提供了这些类型(需要 ISO C11 或 C++ 11 支持)。
但是,您无法直接检查它们是否存在,因为它们是基本类型,而不是宏:
在 C++ 中,char16_t 和 char32_t 是基本类型(因此此标头未在 C++ 中定义此类宏)。
但是,您可以检查 C++ 11 支持。根据比亚恩·斯特鲁斯特鲁普 http://www.stroustrup.com/C++11FAQ.html#11 page:
__cplusplus
C++11 中的宏__cplusplus
将被设置为不同于(大于)当前 199711L 的值。
所以,基本上,你可以这样做:
#if __cplusplus > 199711L
// Has C++ 11, so we can assume presence of `char16_t` and `char32_t`
#else
// No C++ 11 support, define our own
#endif
如何定义自己的?
->Windows 上的 MSVC、ICC:使用特定于平台的类型 https://msdn.microsoft.com/en-us/library/29dh1w7z%28v=vs.80%29.aspx,在 VS .NET 2003 及更高版本中受支持:
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
->Linux 上的 GCC、MinGW、ICC:这些具有完整的 C99 支持,因此请使用来自<cstdint>
并且不要typedef
您自己的(您可能想检查版本或特定于编译器的宏)。
进而:
typedef int16_t char16_t;
typedef uint16_t uchar16_t;
typedef int32_t char32_t;
typedef uint32_t uchar32_t;
如何检查正在使用的编译器?使用这个伟大的页面 http://sourceforge.net/p/predef/wiki/Home/ (“编译器”部分 http://sourceforge.net/p/predef/wiki/Compilers/).