“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

相关推荐
2501_94542480几秒前
调试技巧与核心转储分析
开发语言·c++·算法
m0_579393661 分钟前
单元测试在C++项目中的实践
开发语言·c++·算法
C_Si沉思4 分钟前
C++中的状态模式高级应用
开发语言·c++·算法
j_xxx404_10 分钟前
蓝桥杯基础--递归
数据结构·c++·算法·蓝桥杯·排序算法
左左右右左右摇晃10 分钟前
Java并发——Lock锁
java·开发语言·笔记
tankeven11 分钟前
HJ145 小红背单词
c++·算法
聆风吟º14 分钟前
【C标准库】深入理解C语言memcpy函数:用法、原理与避坑指南
c语言·开发语言·memcpy·库函数
书到用时方恨少!14 分钟前
基于 Three.js 的 3D 地球可视化项目
开发语言·javascript·3d
j_xxx404_15 分钟前
蓝桥杯基础--枚举
数据结构·c++·算法·蓝桥杯
泯仲18 分钟前
RAG系统核心之意图识别与意图树实现全解析
开发语言·大模型·agent·rag