热门标签 | 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被认为是无符号的。


推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 深入解析JVM垃圾收集器
    本文基于《深入理解Java虚拟机:JVM高级特性与最佳实践》第二版,详细探讨了JVM中不同类型的垃圾收集器及其工作原理。通过介绍各种垃圾收集器的特性和应用场景,帮助读者更好地理解和优化JVM内存管理。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文详细探讨了Java中的24种设计模式及其应用,并介绍了七大面向对象设计原则。通过创建型、结构型和行为型模式的分类,帮助开发者更好地理解和应用这些模式,提升代码质量和可维护性。 ... [详细]
  • 主板IO用W83627THG,用VC如何取得CPU温度,系统温度,CPU风扇转速,VBat的电压. ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 在macOS环境下使用Electron Builder进行应用打包时遇到签名验证失败的问题,具体表现为签名后spctl命令检测到应用程序未通过公证(Notarization)。本文将详细探讨该问题的原因及解决方案。 ... [详细]
  • Docker的安全基准
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 本文介绍如何使用 Python 将一个字符串按照指定的行和元素分隔符进行两次拆分,最终将字符串转换为矩阵形式。通过两种不同的方法实现这一功能:一种是使用循环与 split() 方法,另一种是利用列表推导式。 ... [详细]
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • 本文详细介绍了如何在BackTrack 5中配置和启动SSH服务,确保其正常运行,并通过Windows系统成功连接。涵盖了必要的密钥生成步骤及常见问题解决方法。 ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
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社区 版权所有