完全模拟缺失的不同内置类型(特别是:char16_t 和 char32_t)
C++11 有两种新的字符整型数据类型:char16_t
和 char32_t
。我想为没有不同类型的编译器模拟它们,以便重载 I/O 操作以将它们视为字符而不是整数值。
这些是要求:
- 独特(无
typedef
)。 - 正常系统上的精确宽度(ala uint16_t 和 uint32_t)
- 允许其他 C++11 功能(请参见下面的第一次尝试)
- 必须与文字很好地配合;
char16_t c16 = u"blabla unicode text blabla";
必须有效。 - 如果 char16_t 可以在数学运算符中使用,显然这也需要起作用。
我在文字部门失败的第一次尝试是强类型枚举:
enum char16_t : uint16_t;
这还有其他缺点,也许可以通过自己提供必要的运算符来解决(这对我来说真的很好)。
C++11 has two new character integral data types, char16_t
and char32_t
. I would like to emulate them for compilers that don't have a distinct type in order to overload I/O operations to see them a characters instead of their integer value.
These are the requirements:
- Distinct (no
typedef
). - exact-width on normal systems (ala uint16_t and uint32_t)
- other C++11 features are allowed (see below first attempt)
- Must play nice with literals;
char16_t c16 = u"blabla unicode text blabla";
must work. - if char16_t can be used in math operators, obviously this needs to function as well.
My first attempt which fails in the literal department was a strongly typed enum:
enum char16_t : uint16_t;
This has other drawbacks as well, that could perhaps be resolved by supplying the necessary operators myself (which is really fine by me).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不会让初始化工作,因为没有太多的空间来让它工作。问题是您在示例中使用的初始化不应该起作用:字符串文字
u"..."
生成一个char16_t const
对象数组并且您想用它初始化一个指针:此外,如果编译器中没有实现 char16_t ,它就不太可能支持 char16_t 字符串文字。你能实现的最好的结果就是发挥宏观技巧,旨在做正确的事情。目前,您可以使用宽字符文字,当您获得支持 char16_t 的编译器时,您只需更改宏以使用 char16_t 文字即可。即使要使其工作,您可能需要使用大于 16 位的记录类型,因为
wchar_t
在某些平台上使用 32 位。显然,您仍然需要提供各种运算符,例如使整数算术和合适的转换工作。
I don't think you will get the initialization to work because there isn't much scope to get it to work. The problem is that the initialization you are using in your example isn't supposed to work: the string literal
u"..."
yields an an array ofchar16_t const
objects and you want to initialize a pointer with it:Also, without implementation of
char16_t
in the compiler it is very unlikely to supportchar16_t
string literals. The best you could achieve is to play macro tricks which are intended to do the Right Thing. For now, you'd use e.g. wide character literals and when you get a compiler which supportchar16_t
you just change the macro to usechar16_t
literals. Even for this to work you might need to use a record type which is bigger than 16 bit becausewchar_t
uses 32 bits on some platforms.Obviously, you still need to provide all kinds of operators e.g. to make integer arithmetic and suitable conversions work.