热门标签 | 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的基本语法和功能。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文详细探讨了Java中的24种设计模式及其应用,并介绍了七大面向对象设计原则。通过创建型、结构型和行为型模式的分类,帮助开发者更好地理解和应用这些模式,提升代码质量和可维护性。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • Docker的安全基准
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 本文介绍如何使用 Python 将一个字符串按照指定的行和元素分隔符进行两次拆分,最终将字符串转换为矩阵形式。通过两种不同的方法实现这一功能:一种是使用循环与 split() 方法,另一种是利用列表推导式。 ... [详细]
  • Python 异步编程:深入理解 asyncio 库(上)
    本文介绍了 Python 3.4 版本引入的标准库 asyncio,该库为异步 IO 提供了强大的支持。我们将探讨为什么需要 asyncio,以及它如何简化并发编程的复杂性,并详细介绍其核心概念和使用方法。 ... [详细]
  • 深入解析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. ... [详细]
  • 本文详细介绍了如何在BackTrack 5中配置和启动SSH服务,确保其正常运行,并通过Windows系统成功连接。涵盖了必要的密钥生成步骤及常见问题解决方法。 ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 数据管理权威指南:《DAMA-DMBOK2 数据管理知识体系》
    本书提供了全面的数据管理职能、术语和最佳实践方法的标准行业解释,构建了数据管理的总体框架,为数据管理的发展奠定了坚实的理论基础。适合各类数据管理专业人士和相关领域的从业人员。 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
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社区 版权所有