Comments (3)
Could also use a scope guard, no need to hit the heap for that (plus there's one in this library!). Basically the equivalent of:
static void clone (data_type const& source, data_type& data) {
struct scope_guard {
allocator_type& m_alloc;
typename allocator_traits::pointer m_ptr;
bool dismissed = false;
void dismiss() { dismissed = true; }
~scope_guard() { if (!dismissed) allocator_traits::deallocate(m_alloc, m_ptr); }
};
allocator_type alloc { };
auto const& value = *static_cast<Type* const>(source);
auto pointer = allocator_traits::allocate(alloc, 1);
scope_guard guard { 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;
}
from core.
Seems like a very reasonable way to do it without having to add a new class. What do you by "no need to hit the heap"?
from core.
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
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);
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;
}
from core.
Related Issues (20)
- Deprecate usage of MNMLSTC Unittest and replace it with the Catch Framework. HOT 1
- Add build time options to disable exceptions and RTTI HOT 3
- core::basic_string_view constness and substring HOT 1
- string_view::find* functions do not take a position
- Add constexpr and deducing forms of the function objects defined in <functional> HOT 1
- Add make_array and to_array functions (N4315)
- Implement the abs_diff proposal (N4318)
- Result/Expected compliance with standard proposal HOT 2
- variant get() function HOT 3
- core::function_traits<T>::argument<N> always return a type of the 1st argument HOT 2
- Please add VS 2015 support if possible HOT 18
- GCC 5 -Wall issues warning about clang::fallthrough attribute placement in string_view header HOT 3
- default constructible requirement for result<T>::value_type? HOT 4
- string_view::max_size() is doesn't match the standard (revision 7) HOT 1
- SEGV when using small types that are not noexcept copy constructable HOT 2
- `core::get<T>(variant)` fails to compile since 5a0157b
- From documentation, link to the GitHub project page
- Hash of *std::*string_view must equal hash of corresponding std::string object
- Future releases? HOT 1
- Compile error when constructing any with const T* (clang / Mac OS X) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from core.