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


推荐阅读
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文介绍了三种方法来实现在Win7系统中显示桌面的快捷方式,包括使用任务栏快速启动栏、运行命令和自己创建快捷方式的方法。具体操作步骤详细说明,并提供了保存图标的路径,方便以后使用。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • Igotthiscode(IknowitsinSpanishIcantranslateifneeded)wheretheygivemethefunctionS ... [详细]
  • 如何用Vscode和 Visual stdudio配置C++环境
    这篇文章主要讲解了“如何用Vscode和Visualstdudio配置C++环境”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深 ... [详细]
  • ProblemA.MonsterPathCodejamonisamobilegameinwhichmonstertrainer ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了[从头学数学]中第101节关于比例的相关问题的研究和修炼过程。主要内容包括[机器小伟]和[工程师阿伟]一起研究比例的相关问题,并给出了一个求比例的函数scale的实现。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
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社区 版权所有