Search code examples
c++destructorraiiobject-lifetime

Is "premature" destruction possible?


result_t work(resource_t& resource) {
    lock_t ___(resource);

    return work_impl(resource);
}

Is it guaranteed that the destructor of ___ will be called AFTER work_impl() returned? Or is the compiler free to destroy ___ before calling work_impl()?


Solution

  • Expression work_impl(resource) will be executed, the result will be copied to caller side or used as a temporary. Then, Object ___ will be destructed.

    On the other hand, DON'T use __ or ___ as prefix of any identifier. They're reserved for compiler.