“nullptr“ should be used to denote the null pointer

Before C++11, the only way to refer to a null pointer was by using the integer literal 0, which created ambiguity with regard to whether a pointer or an integer was intended. Even with the NULL macro, the underlying value is still 0.

C++11 introduced the keyword nullptr, which is unambiguous and should be used systematically.

Noncompliant Code Example

cpp 复制代码
void f(char *c);
void g(int i);
void h()
{
    f(0); // Noncompliant
    f(NULL); // Noncompliant
    g(0); // Compliant, a real integer
    g(NULL); // Noncompliant, NULL should not be used for a real integer
}
复制代码
 

Compliant Solution

cpp 复制代码
void f(char *c);
void g(int i);
void h()
{
    f(nullptr); // Compliant
    g(0);  // Compliant, a real integer
}

See

For example

相关推荐
姚愚谦17 分钟前
C++中的push_back与emplace_back的区别?
c++
ZC跨境爬虫1 小时前
跟着 MDN 学JavaScript day_9:字符串方法实战挑战与解题思路
开发语言·前端·javascript
青春:一叶知秋2 小时前
【C++】protobuf序列化与反序列化
开发语言·c++
夕除3 小时前
shizhan--10
java·开发语言
Zhang~Ling3 小时前
C++ 红黑树封装:myset和mymap的底层实现
开发语言·数据结构·c++·算法
啦啦啦啦啦zzzz3 小时前
数据结构:堆排序
数据结构·c++·
原来是猿3 小时前
为什么 C++ 需要区分左值和右值?
开发语言·c++
xier_ran3 小时前
【infra之路】PagedAttention
java·开发语言
SilentSamsara4 小时前
NumPy 进阶:广播机制、ufunc 与向量化计算的工程实践
开发语言·python·青少年编程·性能优化·numpy
珊瑚里的鱼4 小时前
C++的强制类型转换
android·开发语言·c++