简体   繁体   English

C99和C++03中空指针定义的区别

[英]Difference in definition of null pointer in C99 and C++03

N2431 is the paper that introduces nullptr . N2431是介绍nullptr的论文。 It says:它说:

The current C++ standard provides the special rule that 0 is both an integer constant and a null pointer constant.当前的 C++ 标准提供了一个特殊规则,即 0 既是整数常量又是空指针常量。 From [C++03] clause 4.10:来自 [C++03] 条款 4.10:

A null pointer constant is an integral constant expression (expr.const) rvalue of integer type that evaluates to zero.空指针常量是整数类型的整数常量表达式 (expr.const) 右值,其计算结果为零。 A null pointer constant can be converted to a pointer type;空指针常量可以转换为指针类型; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type.结果是该类型的空指针值,并且与指向对象的指针或指向函数类型的指针的所有其他值区分开来。 Two null pointer values of the same type shall compare equal.相同类型的两个空指针值比较相等。 The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion (conv.qual).将空指针常量转换为指向 cv 限定类型的指针是一次转换,而不是指针转换后跟限定转换 (conv.qual) 的序列。

This formulation is based on the original K&R C definition and differs from the definition in C89 and C99.此公式基于原始 K&R C 定义,与 C89 和 C99 中的定义不同。 The C standard [C99] says (clause 6.3.2.3): C 标准 [C99] 说(第 6.3.2.3 条):

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.[55]值为 0 的整数常量表达式,或转换为 void * 类型的此类表达式,称为空指针常量。 [55] If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.如果将空指针常量转换为指针类型,则结果指针(称为空指针)保证与指向任何对象或函数的指针不相等。

Let's ignore "[c++03] formulation is based on the original K&R C definition" part and focus on "[c++03 formulation] differs from the definition in [...] C99".让我们忽略“[c++03] 公式是基于原始的 K&R C 定义”部分而关注“[c++03 公式] 与 [...] C99 中的定义不同”。

It is not clear to me in which way do C++03 and C99 definitions differ.我不清楚 C++03 和 C99 定义在哪些方面有所不同。 Yes, they have different formal definitions in the written standard (as we can see the citations differ), but what does that mean from practical perspective?是的,它们在书面标准中有不同的正式定义(正如我们所看到的引用不同),但从实践的角度来看,这意味着什么? Do they really differ?他们真的有区别吗? If I'm reading the article correctly, its tone implies that there are differences.如果我正确阅读这篇文章,它的语气暗示存在差异。 If so, what are they?如果有,它们是什么? I am not able to comprehend/understand the difference based on the citations from standards.我无法理解/理解基于标准引用的差异。

I don't ask specifically how C++03 or C99 null pointer is defined, represented and/or implemented.我没有具体询问 C++03 或 C99 空指针是如何定义、表示和/或实现的。 My question is about how C++03 and C99 null pointer definitions (and perhaps their implementations, if this is important to demonstrate in order to answer this question) differ, and the answer can contain the explanations of definitions of C++03 and C99 null pointers, but only as a helping point to demonstrate the difference or show the lack of it.我的问题是关于 C++03 和 C99 空指针定义(可能还有它们的实现,如果为了回答这个问题而进行演示很重要)有何不同,答案可以包含 C++03 和C99 空指针,但仅作为说明差异或显示差异的帮助点。 That is, only providing the definitions or the explanations of the definitions of each one is not considered an answer to this question.也就是说,仅提供定义或对每个定义的解释不被视为对这个问题的回答。

I am fully aware that nullptr is the preferred way of expressing null pointer in C++11 and later.我完全知道nullptr是在 C++11 及更高版本中表达空指针的首选方式。 This is not the question.这不是问题。

The reason that the standard doesn't allow (void*)0 as a null pointer constant is that a void* is not convertible to any other pointer types (other than cv-qualified void* ).标准不允许(void*)0作为空指针常量的原因是void*不能转换为任何其他指针类型(除了 cv 限定的void* )。 In C you can do void *p = 0; int *ip = p;在 C 中你可以做void *p = 0; int *ip = p; void *p = 0; int *ip = p; . . In C++ the implicit conversion to int* is not allowed.在 C++ 中,不允许隐式转换为int*

In C you have在 C 你有

An integer constant expression with the value 0, or such an expression cast to type void *值为 0 的整数常量表达式,或转换为类型 void * 的此类表达式

which means integer constants with the value of 0 and (void*)0 are valid null pointer constants.这意味着值为0(void*)0整数常量是有效的空指针常量。

In C++ you have在 C++ 中,你有

A null pointer constant is an integral constant expression (expr.const) rvalue of integer type that evaluates to zero空指针常量是整数类型的整数常量表达式 (expr.const) 右值,其计算结果为零

Which means only integer constants with the value of 0 are valid null pointer constants.这意味着只有值为0整数常量才是有效的空指针常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM