通常 C++ 程序必须处理 C 库,这些库提供了用于分配和释放资源的免费函数。为了简化此示例,请考虑两个 C 函数,例如get_resource()
和free_resource()
。
考虑一个对象在其生命周期的某个时间点获取任何资源,并在对象被销毁或由于构造过程中的错误而未完全构造时自动释放它。
获得这种自动化的理想/简短/简单的习语是什么?一个想法如下,但它使对象不能正确地默认移动。有没有更好的方法不意味着从析构函数中释放内存或检查构造函数中的错误以进行回滚?
struct Object {
void* my_get_resource() { // might be called from the constructor or any other point
return get_resource();
}
Object() : up(&resource, &del) {
resource = my_get_resource();
/* init all the stuff, but this init might fail at _many_ points */
}
//~Object() { if (resource) free_resource(resource); } // I don't like: collides with del and is not called if my_get_resource() is called from the constructor and the init process fails for some reasons
private:
void *resource = nullptr;
static void del(void ** ) noexcept {
if (*resource) { free_resource(resource); }
}
unique_ptr < void*, decltype(&del) > up; // RAII: almost good, sadly that makes Object not moveable properly without redefining the move constructor properly
};