Sag*_*gid 9 c++ operator-overloading unique-ptr
我正在处理不是我写的代码。我有这样的声明:
// p is type of std::unique_ptr<uint8_t[]>
if (p < 0) { /* throw an exception */ }
Run Code Online (Sandbox Code Playgroud)
那么p < 0在这种情况下是什么意思呢?
在文档页面,我相信我的情况是16) y < nullptr,这里0是nullptr。
但是它有什么作用呢?
unique_ptr < 0 或者小于运算符做什么?
它与 cppreference 上的重载 (11) 匹配operator<(const unique_ptr&, nullptr_t);。0 隐式转换为std::nullptr_t. 根据文档,结果是std::less<unique_ptr<T,D>::pointer>()(x.get(), nullptr).
结果是实现定义的,但在大多数系统上可能无条件错误。据推测,在 null 不具有 0 的二进制表示形式的奇异系统上,结果可能为 true。
我相信我的情况是 16)
(16) 反之亦然:0 > unique_ptr。结果是一样的。