siw*_*mas 2 c++ unique-ptr c++11
示例没有意义,但仍然无法解释为什么不调用自定义删除器.
在得到答案之后,我编辑了我的代码,因此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
Run Code Online (Sandbox Code Playgroud)
unique_ptr如果包含的指针不是,那么析构函数只会调用它的删除器nullptr.
来自N3337,[unique.ptr.single.dtor]/2
效果: 如果
get() == nullptr没有效果.否则get_deleter()(get()).
| 归档时间: |
|
| 查看次数: |
401 次 |
| 最近记录: |