“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

相关推荐
qq_310658516 分钟前
webrtc代码走读(六)-QOS-FEC冗余度配置
网络·c++·webrtc
golang学习记11 分钟前
Go slog 日志打印最佳实践指南
开发语言·后端·golang
新手村领路人22 分钟前
python opencv gpu加速 cmake msvc cuda编译问题和设置
开发语言·python·opencv
dengzhenyue1 小时前
C# 初级编程
开发语言·c#
Aevget1 小时前
从复杂到高效:QtitanNavigation助力金融系统界面优化升级
c++·qt·金融·界面控件·ui开发
jf加菲猫1 小时前
条款20:对于类似std::shared_ptr但有可能空悬的指针使用std::weak_ptr
开发语言·c++
jf加菲猫2 小时前
条款21:优先选用std::make_unique、std::make_shared,而非直接new
开发语言·c++
scx201310042 小时前
20251019状压DP总结
c++
消失的旧时光-19432 小时前
Kotlin 高阶函数在回调设计中的最佳实践
android·开发语言·kotlin
LucianaiB3 小时前
掌握 Rust:从内存安全到高性能服务的完整技术图谱
开发语言·安全·rust