首页 > 解决方案 > 是否是取消引用指针的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;
}

标签: c++

解决方案


推荐阅读