- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
nullptr"格式正确吗?-6ren"> nullptr"格式正确吗?-给定一个指针p: char *p ; // Could be any type 假设 p 已正确初始化,则格式如下: if (p > 0) // or p > nullptr 更一般地说,当一个操作数-6ren">
给定一个指针p
:
char *p ; // Could be any type
假设 p
已正确初始化,则格式如下:
if (p > 0) // or p > nullptr
更一般地说,当一个操作数是指针而另一个操作数是空指针常量时,使用关系运算符是否格式正确?
最佳答案
在 C++14 中,此代码格式错误,但在 C++14 之前,此代码格式正确(但结果未指定),如 defect report 583: Relational pointer comparisons against the null pointer constant备注:
In C, this is ill-formed (cf C99 6.5.8):
void f(char* s) {
if (s < 0) { }
}...but in C++, it's not. Why? Who would ever need to write (s > 0) when they could just as well write (s != 0)?
This has been in the language since the ARM (and possibly earlier); apparently it's because the pointer conversions (4.10 [conv.ptr]) need to be performed on both operands whenever one of the operands is of pointer type. So it looks like the "null-ptr-to-real-pointer-type" conversion is hitching a ride with the other pointer conversions.
在 C++14 中,N3624是 applied to the draft C++14 standard ,它是 N3478
的修订版。对 583
的建议解决方案说明:
This issue is resolved by the resolution of issue 1512.
问题 1512
建议的解决方法是 N3478
(N3624 是 N3478 的修订版):
The proposed wording is found in document N3478.
第 5.9 节从 C++11 到 C++14 的更改
5.9
部分 关系运算符 在 C++11 draft standard 之间发生了很大变化和 C++14 draft standard ,以下突出显示最相关的差异(强调我的 future ),来自第 1
段:
The operands shall have arithmetic, enumeration, or pointer type, or type std::nullptr_t.
更改为:
The operands shall have arithmetic, enumeration, or pointer type
所以类型 std::nullptr_t不再是有效的操作数,但仍然留下 0
这是一个 空指针常量,因此可以转换(section 4.10
) 到一个指针类型。
这被第 2
段所涵盖,在 C++11 中说:
[...]Pointer conversions (4.10) and qualification conversions (4.4) are performed on pointer operands (or on a pointer operand and a null pointer constant, or on two null pointer constants, at least one of which is non-integral) to bring them to their composite pointer type. If one operand is a null pointer constant, the composite pointer type is std::nullptr_t if the other operand is also a null pointer constant or, if the other operand is a pointer, the type of the other operand.[...]
this 为 空指针常量 操作数显式提供异常,在 C++14 中更改为以下内容:
The usual arithmetic conversions are performed on operands of arithmetic or enumeration type. If both operands are pointers, pointer conversions (4.10) and qualification conversions (4.4) are performed to bring them to their composite pointer type (Clause 5). After conversions, the operands shall have the same type.
在这种情况下,不允许将 0
转换为 指针类型。两个操作数都必须是指针才能应用指针转换,并且要求操作数在转换后具有相同的类型。这在一个操作数是指针类型而另一个是空指针常量 0
的情况下是不满足的。
如果两个操作数都是指针但一个是空指针值怎么办?
R Sahu 提问,下面的代码格式正确吗?:
char* p = "";
char* q = nullptr;
if ( p > q ) {}
是的,在 C++14 中,这段代码格式正确,p
和 q
都是指针,但比较的结果是未指定的。两个指针的定义比较在第 3
段中列出,并说:
Comparing pointers to objects is defined as follows:
If two pointers point to different elements of the same array, or to subobjects thereof, the pointer to the element with the higher subscript compares greater.
If one pointer points to an element of an array, or to a subobject thereof, and another pointer points one past the last element of the array, the latter pointer compares greater.
If two pointers point to different non-static data members of the same object, or to subobjects of such members, recursively, the pointer to the later declared member compares greater provided the two members have the same access control (Clause 11) and provided their class is not a union.
这里没有定义空指针值,稍后在 4
段中它说:
[...]Otherwise, the result of each of the operators is unspecified.
在 C++11 中,它专门使 3
段中未指定结果:
If two pointers p and q of the same type point to different objects that are not members of the same object or elements of the same array or to different functions, or if only one of them is null, the results of p<q, p>q, p<=q, and p>=q are unspecified.
关于c++ - 鉴于 p 是一个指针是 "p > nullptr"格式正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26590267/
关于strcat函数。 while (*p) p++; 和 while (*++p) ; 两者都有效,但是 while (*p++) ; 不起作用。我认为 first 和 th
" in HTML?(HTML中的““是什么
?)
下面例子中的第一行代码是什么。我看到一个YouTuber在写下面的代码,它显示了一个设计在csswar Challenges中。我也尝试了一下,它很管用。但我以前从未在任何HTML教程上看到过它,我在
vs.
是不间断空格,表示没有换行的空白处。 如果我用 我在两个段落之间有一个空格(更大的间隔)。如果我使用 我在两个段落之间只有一个新行(没有中断)。为什么? 最佳答案 在 HTML 中
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 9
我对编程还很陌生,只是想知道为什么这段代码: for ( ; *p; ++p) *p = tolower(*p); 当 p 指向一个字符串时,可以降低 c 中字符串的大小写吗? 最佳答案 一般来说,这
代码 int n = 25; int *p = &n; printf("%x\n %d\n %x\n", p, p[0], p[1]); 返回: \ 当然我永远不会这样做,但在 K&R 中声明
所以,我想创建一个简单的程序,返回有关连续素数的计算结果。首先,我创建一个包含所有这些素数的列表,然后尝试计算结果,但这给了我一个超出范围的索引。有人可以帮助我吗?我的程序: primes = []
这个问题在这里已经有了答案: With arrays, why is it the case that a[5] == 5[a]? (20 个答案) 关闭 9 年前。 我想知道 C/C++ 中以下四
我仍在努力理解 *p、&p 和 p 之间的区别。根据我的理解,* 可以被认为是“指向的值”,而 & 可以被认为是“地址”。换句话说,* 保存值,而 & 保存地址。如果这是真的,那么 *p 和 p 之间
你是吗? [xxxrecipientFirstNamexxx]
和你是吗? {recipientFirstName}
需要更换 你是吗? [xxxrecipientFirstNamexxx] 和 你是吗? {recipientFirstName} 。我尝试使用边界匹配器。但结果并不符合预期。我尝试使用下面的代码 "A
我想按 IsTop 属性升序排序对象,然后按 JobId 属性降序排序: query = query.OrderBy(p => p.IsTop).ThenOrderByDescending(p =
在我尝试使用 Apache POI 进行转换的 Excel 文件中,我有一个单元格的数值为 -3.97819466831428,自定义格式为“0.0 p.p.;(0.0 p.p.)”。因此,在 Exc
我想创建一个扩展方法,允许我调用 ToSerializableDictionary(p => p.ID)而不是 .ToDictionary(p => p.ID)在以下 LINQ 上下文中。虽然我不确定
在下面的 HTML 代码上运行此 jQuery 代码会返回不同的结果,我认为它们应该返回相同的值。 jQuery 代码: var counter = 0; $("p").each(function()
在下面的代码片段中,符号 *p 等同于 p[0],*(p + 1) 等同于p[1],依此类推。 int* p = new int[3] { 1, 2, 3}; cout << *p << ' ' <<
这个问题在这里已经有了答案: What will happen when I call a member function on a NULL object pointer? [duplicate]
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Undefined Behavior and Sequence Points 按照标准中的定义,E1 +=
" in HTML?(在HTML中“
以下示例中的第一行代码是什么。我看到一个youtube用户写下面的代码,它显示在cssbattle挑战的设计。我也试过,它的作品。但我从来没有见过它在任何HTML教程之前,我在谷歌上搜索它,但它只显示
每当我收到来自 MS outlook 的电子邮件时,我都会收到此标记 & nbsp ; (没有空格)哪个显示为?在 <>. 当我将其更改为 ISO-8859-1 时,浏览器页面字符集编码为 UTF-8
p1
TESTp2
代码: from bs4 import BeautifulSoup soup = BeautifulSoup('p1TESTp2') print soup.div() 结果: [p1, p2] 为什么
我是一名优秀的程序员,十分优秀!