简体   繁体   English

C ++ 11数据类型混乱

[英]C++11 data types confusion

I am trying to write a solid summary for C++ datatypes, but I have some confusion about the new datatypes. 我正在尝试为C ++数据类型编写一个可靠的摘要,但我对新数据类型有些困惑。

As I understood from my readings about C++ data types, char16_t and char_32_t are fundamental data types and part of the core language since C++11. 正如我从C ++数据类型的读数中所理解的那样, char16_tchar_32_t是基础数据类型,也是自C ++ 11以来核心语言的一部分。

It is mentioned that they are distinct data types. 提到它们是不同的数据类型。

Q1: What exactly does "distinct" mean here? Q1:“鲜明”究竟是什么意思?

Q2: Why intxx_t type family like int32_t was chosen not to be a fundamental datatype? Q2:为什么选择像int32_t这样的intxx_t类型族不是基本数据类型? And how can they be beneficial when choosing them instead of int ? 在选择它们而不是int时,它们如何才有益?

The charXX_t types were introduced in N2249 . charXX_t类型在N2249中引入。 They are created as a distinct type from uintXX_t to allow overloading: 它们是作为uintXX_t的不同类型创建的,以允许重载:

Define char16_t to be a distinct new type, that has the same size and representation as uint_least16_t . char16_t定义为一个独特的新类型,其大小和表示形式与uint_least16_t相同。 Likewise, define char32_t to be a distinct new type, that has the same size and representation as uint_least32_t . 同样,将char32_t定义为一个独特的新类型,它具有与uint_least32_t相同的大小和表示uint_least32_t

[N1040 defined char16_t and char32_t as typedefs to uint_least16_t and uint_least32_t , which make overloading on these characters impossible. [N1040将char16_tchar32_t定义为uint_least16_tuint_least32_t typedef, 这使得不能对这些字符进行重载。 ] ]

To answer the second part of the question: 要回答问题的第二部分:

The fixed size integer types are inherited from C, where they are typedef s. 固定大小的整数类型继承自C,它们是typedef It was decided to keep them as typedef s to be compatible. 决定将它们作为typedef保持兼容。 Note that the C language doesn't have overloaded functions, so the need for "distinct" types is lower there. 请注意,C语言没有重载函数,因此对“不同”类型的需求较低。

One reason for using int32_t is that you need one or more of its required properties: 使用int32_t一个原因是您需要一个或多个必需属性:

Signed integer type with width of exactly 32 bits with no padding bits and using 2's complement for negative values. 有符号整数类型,宽度正好为 32位,没有填充位,负值使用2的补码。

If you use an int it might, for example , be 36 bits and use 1's complement. 如果您使用的int它可能, 例如 ,是36位,并使用1的补数。

However, if you don't have very specific requirements, using a normal int will work fine. 但是,如果您没有非常具体的要求,使用普通的int将正常工作。 One advantage is that an int will be available on all systems, while the 36-bit machine (or a 24 bit embedded processor) might not have any int32_t at all. 一个优点是int将在所有系统上可用,而36位机器(或24位嵌入式处理器)可能根本没有任何int32_t

To answer your Q1: 回答你的Q1:

Distinct type means std::is_same<char16_t,uint_least16_t>::value is equal to false . Distinct类型表示std::is_same<char16_t,uint_least16_t>::value等于false

So overloaded functions are possible. 所以重载功能是可能的。

(There is no difference in size, signedness, and alignment, though.) (但是,在大小,签名和对齐方面没有区别。)

Other way to express "distinct types" is that you can create two overloaded functions for each type. 表达“不同类型”的其他方式是您可以为每种类型创建两个重载函数。 For instance: 例如:

typedef int Int;
void f(int) { impl_1; }
void f(Int) { impl_2; }

If you try to compile a code snippet containing both functions, the compiler will complain about a ODR violation: you are trying to redefine the same function twice, since their arguments are the same. 如果您尝试编译包含这两个函数的代码片段,编译器将抱怨ODR违规:您尝试重新定义相同的函数两次,因为它们的参数是相同的。 That's because typedef s doesn't create types, just aliases. 那是因为typedef不会创建类型,只是别名。

However, when types are truly distinct, both versions will be seen as two different overloads by the compiler. 但是,当类型真正不同时,编译器会将这两个版本视为两个不同的重载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM