热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

std::is_unsigned::价值定义良好的?-Isstd::is_unsigned::valuewelldefined?

Iamwonderingwhether我想知道是否std::is_unsigned<bool>::valueiswelldefinedaccordingtot

I am wondering whether

我想知道是否

std::is_unsigned::value

is well defined according to the standard or not?

是否按照标准来定义?

I ask the question because typename std::make_unsigned::type is not well defined.

我问这个问题是因为typename std::make_unsigned :::type没有很好的定义。

4 个解决方案

#1


62  

There is no concept of signedness for bool. From [basic.fundamental]/6:

bool没有签名的概念。从[basic.fundamental]/ 6:

Values of type bool are either true of false. [Note: There are no signed, unsigned, short, or long bool types or values. — end note] Values of type bool participate in integral promotions (4.5).

bool类型的值要么为真,要么为假。[注:没有符号、无符号、短或长bool类型或值。-尾注]bool类型的值参与整体促销(4.5)。

By contrast, signedness is explicitly called out for the signed integer types (paragraph 2) and unsigned integer types (paragraph 3).

相比之下,对带符号整数类型(第2段)和无符号整数类型(第3段)显式地调用签名。

Now for the is_signed and is_unsigned traits. First off, the traits are always well-defined, but only interesting for arithmetic types. bool is an arithmetic type, and is_signed::value is defined (see Table 49) as T(-1) . By using the rules of boolean conversion and standard arithmetic conversions, we see that this is is false for T = bool (because bool(-1) is true, which converts to 1). Similarly, is_unsigned::value is defined as T(0) , which is true for T = bool.

现在对于is_signed和is_unsigned特性。首先,这些特征总是定义良好,但只对算术类型有兴趣。bool是一种算术类型,is_signed :::value被定义为T(-1)

#2


23  

is_unsigned is defined in [meta.unary.comp]/2 as

is_unsigned在[元。unary.comp]/2 as中定义

If is_arithmetic::value is true, the same result as
bool_constant::value; otherwise, false

如果is_算数 ::value为真,结果与bool_constant ::值相同;否则,假

bool is clearly an arithmetic type (being integral). Now consider [conv.bool]/1:

bool显然是一个算术类型(作为积分)。现在考虑[conv.bool]/ 1:

A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

将零值、空指针值或空成员指针值转换为false;任何其他值都被转换为true。

I.e. bool(0) is equivalent to false , and the latter holds since the values are promoted to 0 and 1, respectively.

也就是说,bool(0)

Thus is_unsigned::value is true (and, conversely, is_signed is false), due to the fact that boolean values correspond to the unsigned values 0 and 1 during arithmetic operations. However, it doesn't really make sense to assess bool's signedness, much less perform make_unsigned on it, since it doesn't represent integers, but rather states.

因此is_unsigned ::value是true(反之,is_signed是false),因为在算术操作期间,布尔值对应于无符号值0和1。然而,评估bool的签名并没有什么意义,更不用说对它执行make_unsigned了,因为它不是整数,而是状态。


: The fact that this template is applicable to bool in the first place is determined by its Requirement clause being non-existent, bool not being an incomplete type ([res.on.functions]/(2.5)) and no other requirements being mentioned in [meta.rqmts] for UnaryTypeTraits.

#3


10  

Yes, it is well-defined, as is any other unary type trait.

是的,它是明确的,就像任何其他一元类型特征一样。

C++14 (n4140) 20.10.4/2 "Unary type traits" mandates:

C+ 14 (n4140) 20.10.4/2“一元类型特征”

Each of these templates shall be a UnaryTypeTrait (20.10.1) with a BaseCharacteristic of true_type if the corresponding condition is true, otherwise false_type.

如果相应的条件为true,则每个模板都应该是一个UnaryTypeTrait(20.10.1),如果对应的条件为true,则true_type的BaseCharacteristic为true_type,否则为false type。

20.10.1/1:

20.10.1/1:

A UnaryTypeTrait describes a property of a type. It shall be a class template that takes one template type argument and, optionally, additional arguments that help define the property being described. It shall be DefaultConstructible, CopyConstructible, and publicly and unambiguously derived, directly or indirectly, from its BaseCharacteristic, which is a specialization of the template integral_constant (20.10.3), with the arguments to the template integral_constant determined by the requirements for the particular property being described. The member names of the BaseCharacteristic shall not be hidden and shall be unambiguously available in the UnaryTypeTrait.

不arytype trait描述类型的属性。它应该是一个类模板,它接受一个模板类型参数,并且可以选择接受帮助定义所描述的属性的附加参数。它应该是默认可构造的、可复制的,并且直接或间接地、公开地、明确地从它的基属性派生出来,基属性是integral_constant模板的专门化(20.10.3),模板integral_constant的参数由所描述的特定属性的需求决定。基性特征的成员名称不应被隐藏,并且在unarytype特征中应该是明确可用的。

From this it follows that the construct std::is_unsigned::value has to be well-defined for any type T, whether the concept of "signedness" makes sense for the type or not.

由此可以得出这样的结论:构造std::is_unsigned :::value必须为任何类型T定义良好的定义,无论“签名权”的概念对类型是否有意义。

#4


9  

Yes it is well defined and the result should be std::is_unsigned::value == true

是的,它的定义很好,结果应该是std::is_unsigned ::value == true。

The documentation for std::is_signed says

std::is_signed说的文档

If T is a signed arithmetic type, provides the member constant value equal true. For any other type, value is false.

如果T是一个有符号的算术类型,则提供成员常量值等于true。对于任何其他类型,值都是假的。

So then if you look at std::is_arithmetic

如果你看看std: is_算术

If T is an arithmetic type (that is, an integral type or a floating-point type), provides the member constant value equal true. For any other type, value is false.

如果T是一种算术类型(即整数类型或浮点类型),则提供成员常量值为true。对于任何其他类型,值都是假的。

Which finally leads to std::is_integral

这最终导致std::is_integral ?

Checks whether T is an integral type. Provides the member constant value which is equal to true, if T is the type bool, char, char16_t, char32_t, wchar_t, short, int, long, long long, or any implementation-defined extended integer types, including any signed, unsigned, and cv-qualified variants. Otherwise, value is equal to false.

检查T是否为整数类型。如果T是类型bool、char、char16_t、char32_t、char32_t、wchar_t、short、int、long、long、long、long、long,或者任何实现定义的扩展整数类型,包括任何已签名的、未签名的和cv限定的变体,那么提供的成员常量值就等于true。否则,值等于false。

Interestingly, there is another function std::numeric_limits::is_signed that states

有趣的是,还有一个std::numeric_limits::is_signed that states。

The value of std::numeric_limits::is_signed is true for all signed arithmetic types T and false for the unsigned types. This constant is meaningful for all specializations.

std:::numeric_limits ::is_signed为所有有符号算术类型T的真值,无符号类型为假值。这个常数对所有的专门化都有意义。

Where the specialization for bool is listed as false, which also confirms that bool is considered unsigned.

其中bool的专门化被列出为false,这也确认了bool被认为是无符号的。


推荐阅读
author-avatar
我爱宝宝们小童鞋_244_571_742
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有