Add back static_asserts, remove confusing +

This commit is contained in:
frederick-vs-ja 2022-01-10 23:48:26 +08:00 committed by GitHub
parent 14ba5d87f3
commit f7f99daded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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