gpt4 book ai didi

c++ - C++11 中的 char16_t 和 char32_t 类型

转载 作者:太空狗 更新时间:2023-10-29 20:41:33 26 4
gpt4 key购买 nike

据我了解,在 C++11 中 char16_tchar32_t不是 typedef,而是新的原始数据类型。 (如有错误请指正)。

我正在编写一个用于 linux gcc 和 Windows Visual Studio 的库。所以我写了一个特征:

template <class T>
struct IsChar : std::false_type {
};

template <>
struct IsChar<char> : std::true_type {
};

template <>
struct IsChar<wchar_t> : std::true_type {
};

template <>
struct IsChar<char16_t> : std::true_type {
};

template <>
struct IsChar<char32_t> : std::true_type {
};

但是,我注意到在 VS (2013) 中,char16_tchar32_t只是 unsigned short 的类型定义和 unsigned[..]\Microsoft Visual Studio 12.0\VC\include\yvals.h .

所以我检查了一下

std::is_same<char32_t, unsigned>::value

true在 VS 和 false在 gcc 中。

所以我的问题是:

  1. char16_tchar32_t标准中的新原始类型(这两种类型的 VS 实现是非标准的吗?)
  2. 这两种类型是 gcc 中的新基元类型,还是只是另一种类型定义(但可能与 VS 中的类型定义不同)?'
  3. VS 是否计划在不久的将来将这些类型实现为新的基本类型?

最佳答案

对于 1:它们是不同的类型

3.9.1 Fundamental types [basic.fundamental]

5 Type wchar_t is a distinct type whose values can represent distinct codes for all members of the largest extended character set specified among the supported locales (22.3.1). Type wchar_t shall have the same size, signedness, and alignment requirements (3.11) as one of the other integral types, called its underlying type. Types char16_t and char32_t denote distinct types with the same size, signedness, and alignment as uint_least16_t and uint_least32_t, respectively, in <cstdint>, called the underlying types.

(强调我的)

这意味着GCC是正确的,VS是错误的。

对于 2:GCC 按预期实现它们,即作为不同的类型。

对于 3:只有 Microsoft 可以回答这个问题,但我认为他们很可能会正确实现它们。编译器供应商有时会简单地使用 hack 作为变通方法来弥合差距,直到准备好完整的解决方案。

关于c++ - C++11 中的 char16_t 和 char32_t 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20958262/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com