mirror of
https://github.com/google/leveldb.git
synced 2025-01-27 06:30:07 +08:00
Add back static_assert
s, remove confusing +
This commit is contained in:
parent
14ba5d87f3
commit
f7f99daded
@ -10,6 +10,11 @@
|
||||
|
||||
namespace leveldb {
|
||||
|
||||
template <std::size_t Size, std::size_t Alignment>
|
||||
struct ObjectBuffer {
|
||||
alignas(Alignment) unsigned char data_[Size];
|
||||
};
|
||||
|
||||
// Wraps an instance whose destructor is never called.
|
||||
//
|
||||
// This is intended for use with function-level static variables.
|
||||
@ -18,7 +23,12 @@ class NoDestructor {
|
||||
public:
|
||||
template <typename... ConstructorArgTypes>
|
||||
explicit NoDestructor(ConstructorArgTypes&&... constructor_args) {
|
||||
::new (static_cast<void*>(instance_storage_))
|
||||
static_assert(sizeof(instance_storage_) >= sizeof(InstanceType),
|
||||
"instance_storage_ is not large enough to hold the instance");
|
||||
static_assert(
|
||||
alignof(decltype(instance_storage_)) >= alignof(InstanceType),
|
||||
"instance_storage_ does not meet the instance's alignment requirement");
|
||||
::new (static_cast<void*>(instance_storage_.data_))
|
||||
InstanceType(std::forward<ConstructorArgTypes>(constructor_args)...);
|
||||
}
|
||||
|
||||
@ -28,11 +38,11 @@ class NoDestructor {
|
||||
NoDestructor& operator=(const NoDestructor&) = delete;
|
||||
|
||||
InstanceType* get() {
|
||||
return reinterpret_cast<InstanceType*>(+instance_storage_);
|
||||
return reinterpret_cast<InstanceType*>(instance_storage_.data_);
|
||||
}
|
||||
|
||||
private:
|
||||
alignas(InstanceType) unsigned char instance_storage_[sizeof(InstanceType)];
|
||||
ObjectBuffer<sizeof(InstanceType), alignof(InstanceType)> instance_storage_;
|
||||
};
|
||||
|
||||
} // namespace leveldb
|
||||
|
Loading…
Reference in New Issue
Block a user