Closed xiaozhuai closed 1 year ago
Why not just use a smart pointer (unique_ptr)?
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> finally(server_info, &freeaddrinfo);
Why not just use a smart pointer (unique_ptr)?
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> finally(server_info, &freeaddrinfo);
~the memory allocation is not neccessary.~ it's semantic doesn't make sense, sounds tricky.
Why not just use a smart pointer (unique_ptr)?
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> finally(server_info, &freeaddrinfo);
Using a unique_ptr can't do this (multiple clean up)
finally {
delete[] aaa;
free(bbb);
freeaddrinfo(info);
};
And in this case, unlock a mutex, If using a unique_ptr, it would be more complex and tricky.
finally {
mutex.unlock();
};
And also, using lambda is better for compiler to do (inline) optimization.
addrinfo
, it's not reuseable.finally
is simple, clean, safer, and higher reuseable.finally
macro implement something like golang'sdefer
keyword, but it will exec when leaving current scope, not function.