Skip to content

Commit 82c3104

Browse files
leveldb Teamfanquake
authored andcommitted
Fix C++23 compilation errors in leveldb
Remove usages of std::aligned_storage, which is deprecated. More details about the replacement in https://crbug.com/388068052. PiperOrigin-RevId: 713346733
1 parent 04b5790 commit 82c3104

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

util/env_posix.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,11 @@ class SingletonEnv {
854854
#endif // !defined(NDEBUG)
855855
static_assert(sizeof(env_storage_) >= sizeof(EnvType),
856856
"env_storage_ will not fit the Env");
857-
static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType),
857+
static_assert(std::is_standard_layout_v<SingletonEnv<EnvType>>);
858+
static_assert(
859+
offsetof(SingletonEnv<EnvType>, env_storage_) % alignof(EnvType) == 0,
860+
"env_storage_ does not meet the Env's alignment needs");
861+
static_assert(alignof(SingletonEnv<EnvType>) % alignof(EnvType) == 0,
858862
"env_storage_ does not meet the Env's alignment needs");
859863
new (&env_storage_) EnvType();
860864
}
@@ -872,8 +876,7 @@ class SingletonEnv {
872876
}
873877

874878
private:
875-
typename std::aligned_storage<sizeof(EnvType), alignof(EnvType)>::type
876-
env_storage_;
879+
alignas(EnvType) char env_storage_[sizeof(EnvType)];
877880
#if !defined(NDEBUG)
878881
static std::atomic<bool> env_initialized_;
879882
#endif // !defined(NDEBUG)

util/env_windows.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,11 @@ class SingletonEnv {
802802
#endif // !defined(NDEBUG)
803803
static_assert(sizeof(env_storage_) >= sizeof(EnvType),
804804
"env_storage_ will not fit the Env");
805-
static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType),
805+
static_assert(std::is_standard_layout_v<SingletonEnv<EnvType>>);
806+
static_assert(
807+
offsetof(SingletonEnv<EnvType>, env_storage_) % alignof(EnvType) == 0,
808+
"env_storage_ does not meet the Env's alignment needs");
809+
static_assert(alignof(SingletonEnv<EnvType>) % alignof(EnvType) == 0,
806810
"env_storage_ does not meet the Env's alignment needs");
807811
new (&env_storage_) EnvType();
808812
}
@@ -820,8 +824,7 @@ class SingletonEnv {
820824
}
821825

822826
private:
823-
typename std::aligned_storage<sizeof(EnvType), alignof(EnvType)>::type
824-
env_storage_;
827+
alignas(EnvType) char env_storage_[sizeof(EnvType)];
825828
#if !defined(NDEBUG)
826829
static std::atomic<bool> env_initialized_;
827830
#endif // !defined(NDEBUG)

util/no_destructor.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_
66
#define STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_
77

8+
#include <cstddef>
89
#include <type_traits>
910
#include <utility>
1011

@@ -20,8 +21,12 @@ class NoDestructor {
2021
explicit NoDestructor(ConstructorArgTypes&&... constructor_args) {
2122
static_assert(sizeof(instance_storage_) >= sizeof(InstanceType),
2223
"instance_storage_ is not large enough to hold the instance");
24+
static_assert(std::is_standard_layout_v<NoDestructor<InstanceType>>);
2325
static_assert(
24-
alignof(decltype(instance_storage_)) >= alignof(InstanceType),
26+
offsetof(NoDestructor, instance_storage_) % alignof(InstanceType) == 0,
27+
"instance_storage_ does not meet the instance's alignment requirement");
28+
static_assert(
29+
alignof(NoDestructor<InstanceType>) % alignof(InstanceType) == 0,
2530
"instance_storage_ does not meet the instance's alignment requirement");
2631
new (&instance_storage_)
2732
InstanceType(std::forward<ConstructorArgTypes>(constructor_args)...);
@@ -37,8 +42,7 @@ class NoDestructor {
3742
}
3843

3944
private:
40-
typename std::aligned_storage<sizeof(InstanceType),
41-
alignof(InstanceType)>::type instance_storage_;
45+
alignas(InstanceType) char instance_storage_[sizeof(InstanceType)];
4246
};
4347

4448
} // namespace leveldb

0 commit comments

Comments
 (0)