示例没有意义,但仍然无法解释为什么未调用自定义删除器。
得到答案后,我编辑了代码,因此myP
在smartP
超出范围之前不能为null
int * myP = NULL;
{
std::unique_ptr<int, std::function<void(int*)>> smartP(myP, [](int * a) {
*a = 8; // Custom deleter that is trying to dereference NULL never called
});
int a = 9;
myP = &a;
} // smartP goes out of scope, I expect custom deleter to be called
unique_ptr
如果包含的指针不是,则析构函数的析构函数将仅调用其删除器nullptr
。
从N3337起,[unique.ptr.single.dtor] / 2
效果: 如果
get() == nullptr
没有效果。否则get_deleter()(get())
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句