c++ - 是否是取消引用指针的UB?
问题描述
我有一些代码,但我不明白是否存在取消引用指针的 UB。它工作正常,但我有疑问。
#include <iostream>
#include <cstdlib>
template <class T>
struct NullRef
{
static T& null(void)
{
static T* p(nullptr);
return *p;
}
static bool isNull(const T& t)
{
return &t == &null();
}
};
bool foo(const int& r = NullRef<int>::null()) {
return NullRef<int>::isNull(r);
}
int main()
{
std::cout << "isNull = "<<foo() << std::endl;
return 0;
}
解决方案
推荐阅读
- java - 将文本正确插入 Android 应用程序
- sql - 加入查询和子查询
- awk - 使用 awk 根据范围合并两个文件
- javascript - ReactJs中没有{Component}的删除按钮
- php - Nesbot Carbon diffInHours 的奇怪结果
- inno-setup - 为什么 Inno Setup Preprocessor:#emit 页面上多了一对花括号?
- python - 查找字符串并在同一列中返回不同的字符串
- javascript - 使用 puppeteer 获取自定义元素,然后执行单击操作
- c# - 通过 Windows 服务连接 SQL Server 2012
- regex - awk 无法拆分大文件 (10Gb+)