使用char16_t,char32_t等无C ++ 11? [英] Using char16_t, char32_t etc without C++ 11?

查看:835
本文介绍了使用char16_t,char32_t等无C ++ 11?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有固定的宽度类型,包括字符类型。 < stdint.h> 提供整数类型,但不提供字符,除非使用C ++ 11,我不能这样做。



有一个干净的方式来定义这些类型( char16_t char32_t 等)没有与C ++ 11定义的冲突,如果源将与C ++ 11混合?



谢谢:)

解决方案

我认为检查这些类型是否是支持平台的。例如,如果提供这些类型(需要ISO C11或C ++ 11支持),则GCC定义: __ CHAR16_TYPE __ __ CHAR32_TYPE __



但是,您不能直接检查它们的存在,因为它们是基本类型,而不是宏:


在C ++中,char16_t和char32_t是基本类型(因此这个头在C ++中不定义这样的宏)。


但是,您可以检查C ++ 11支持。根据 Bjarne Stroustrup的页面:



/ code>将被设置为不同于(大于)当前199711L的值。


基本上,你可以这样做:

 code> #if __cplusplus> 199711L 
//有C ++ 11,所以我们可以假设存在`char16_t`和`char32_t`
#else
//没有C ++ 11支持,定义我们自己的
#endif

如何定义自己的?



strong> - > MSVC,ICC在Windows上:使用平台特定类型,在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;

如何检查使用的编译器?使用这个精彩网页'编译器'部分)。


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

Is there a clean way to define these types (char16_t, char32_t, etc) without conflicting with those defined by C++11 in case the source would ever be mixed with C++11 ?

Thank you :)

解决方案

Checking whether this types are supported is a platform-dependent thing, I think. For example, GCC defines: __CHAR16_TYPE__ and __CHAR32_TYPE__ if these types are provided (requires either ISO C11 or C++ 11 support).

However, you cannot check for their presence directly, because they are fundamental types, not macros:

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

However, you could check for C++ 11 support. According to Bjarne Stroustrup's page:

__cplusplus

In C++11 the macro __cplusplus will be set to a value that differs from (is greater than) 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 define your own?

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

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

-> GCC, MinGW, ICC on Linux: these have full C99 support, so use types from <cstdint> and don't typedef your own (you may want to check version or compiler-specific macro).

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 what compiler is in use? Use this great page ('Compilers' section).

这篇关于使用char16_t,char32_t等无C ++ 11?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆