Using char16_t, char32_t etc. Without C ++ 11?

I would like to have fixed width types, including character types. <stdint.h> provides types for integers, but not characters, unless using C ++ 11, which I cannot do.

Is there an easy way to define these types ( char16_t , char32_t , etc.), not inconsistent with those defined by C ++ 11 if the source is mixed with C ++ 11?

Thanks:)

+4
source share
1 answer

Checking if these types are supported is platform dependent. For example, GCC defines: __CHAR16_TYPE__ and __CHAR32_TYPE__ if these types are provided (ISO C11 or C ++ 11 support required).

However, you cannot directly check your presence, because these are fundamental types, not macros:

In C ++, char16_t and char32_t are fundamental types (and therefore this header does not define such macros in C ++).

However, you can check for C ++ 11 support. According to Bjarne Stroustrup :

__ cplusplus

In C ++ 11, the __cplusplus macro will be set to a value that differs from (more) the current 199711L.

So basically you could do:

 #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 

How to define your own?

MSVC, ICC on Windows: use platform-specific types supported in VS.NET 2003 and later:

 typedef __int16 int16_t; typedef unsigned __int16 uint16_t; typedef __int32 int32_t; typedef unsigned __int32 uint32_t; 

GCC, MinGW, ICC on Linux: they have full C99 support, so use types from <cstdint> and not typedef your own (you can check the version or macro of the compiler).

And then:

 typedef int16_t char16_t; typedef uint16_t uchar16_t; typedef int32_t char32_t; typedef uint32_t uchar32_t; 

How to check which compiler is used? Use this wonderful page ( section "Compilers" ).

+7
source

Source: https://habr.com/ru/post/971242/


All Articles