You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
static void clone (data_type const& source, data_type& data) {
allocator_type alloc { };
auto const& value = *static_cast<Type* const>(source);
auto pointer = allocator_traits::allocate(alloc, 1);
allocator_traits::construct(alloc, pointer, value);
data = pointer;
}
If the construction throws, the allocated memory is leaked. The best way (I've seen) to work around this issues is to store the allocated memory in a unique_ptr with a custom dtor that simply calls allocator::deallocate.
For reference here is how I solved this problem in my any implementation (Which is based off of libc++'s std::function).
Whoops for some reason I thought unique_ptr would be making a heap allocation but obviously it doesn't. It's been a long day haha. It does fit the use-case for scope_guard very nicely though (basically unique_ptr is acting like one).
Essentially both of our examples are doing the same thing, but with the scope_guard in this library (after it adds dismissal support) it'd be
staticvoidclone (data_type const& source, data_type& data) {
allocator_type alloc { };
autoconst& value = *static_cast<Type* const>(source);
auto pointer = allocator_traits::allocate(alloc, 1);
auto guard = make_scope_guard([&] { allocator_traits::deallocate(alloc, pointer); });
allocator_traits::construct(alloc, pointer, value);
guard.dismiss(); // if an exception throws, the memory will be deallocated in the guard dtor
data = pointer;
}
If the construction throws, the allocated memory is leaked. The best way (I've seen) to work around this issues is to store the allocated memory in a unique_ptr with a custom dtor that simply calls allocator::deallocate.
For reference here is how I solved this problem in my any implementation (Which is based off of libc++'s std::function).
The text was updated successfully, but these errors were encountered: