Skip to content

Commit 0bd8438

Browse files
derekmaurocopybara-github
authored andcommitted
Remove pre-C++17 workarounds for lack of std::launder
PiperOrigin-RevId: 734142314 Change-Id: Ib1308def5a6ab9be8890c6a83e793d298b3c777f
1 parent bdbaf37 commit 0bd8438

7 files changed

+5
-46
lines changed

absl/base/no_destructor.h

+4-23
Original file line numberDiff line numberDiff line change
@@ -161,32 +161,13 @@ class NoDestructor {
161161
new (&space_) T(std::forward<Args>(args)...);
162162
}
163163
absl::Nonnull<const T*> get() const {
164-
return Launder(reinterpret_cast<const T*>(&space_));
164+
return std::launder(reinterpret_cast<const T*>(&space_));
165165
}
166-
absl::Nonnull<T*> get() { return Launder(reinterpret_cast<T*>(&space_)); }
167-
168-
private:
169-
template <typename P>
170-
static absl::Nonnull<P*> Launder(absl::Nonnull<P*> p) {
171-
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
172-
return std::launder(p);
173-
#elif ABSL_HAVE_BUILTIN(__builtin_launder)
174-
return __builtin_launder(p);
175-
#else
176-
// When `std::launder` or equivalent are not available, we rely on
177-
// undefined behavior, which works as intended on Abseil's officially
178-
// supported platforms as of Q3 2023.
179-
#if defined(__GNUC__) && !defined(__clang__)
180-
#pragma GCC diagnostic push
181-
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
182-
#endif
183-
return p;
184-
#if defined(__GNUC__) && !defined(__clang__)
185-
#pragma GCC diagnostic pop
186-
#endif
187-
#endif
166+
absl::Nonnull<T*> get() {
167+
return std::launder(reinterpret_cast<T*>(&space_));
188168
}
189169

170+
private:
190171
alignas(T) unsigned char space_[sizeof(T)];
191172
};
192173

absl/container/btree_test.cc

-3
Original file line numberDiff line numberDiff line change
@@ -2674,8 +2674,6 @@ TEST(Btree, HeterogeneousInsertOrAssign) {
26742674
}
26752675
#endif
26762676

2677-
// This test requires std::launder for mutable key access in node handles.
2678-
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
26792677
TEST(Btree, NodeHandleMutableKeyAccess) {
26802678
{
26812679
absl::btree_map<std::string, std::string> map;
@@ -2701,7 +2699,6 @@ TEST(Btree, NodeHandleMutableKeyAccess) {
27012699
EXPECT_THAT(map, ElementsAre(Pair("key", "mapped")));
27022700
}
27032701
}
2704-
#endif
27052702

27062703
struct MultiKey {
27072704
int i1;

absl/container/flat_hash_map_test.cc

-3
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,6 @@ TEST(FlatHashMap, CForEachMutate) {
360360
}
361361
}
362362

363-
// This test requires std::launder for mutable key access in node handles.
364-
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
365363
TEST(FlatHashMap, NodeHandleMutableKeyAccess) {
366364
flat_hash_map<std::string, std::string> map;
367365

@@ -373,7 +371,6 @@ TEST(FlatHashMap, NodeHandleMutableKeyAccess) {
373371

374372
EXPECT_THAT(map, testing::ElementsAre(Pair("key", "mapped")));
375373
}
376-
#endif
377374

378375
TEST(FlatHashMap, Reserve) {
379376
// Verify that if we reserve(size() + n) then we can perform n insertions

absl/container/internal/common_policy_traits.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ struct common_policy_traits {
119119
old_slot)) {
120120
return P::transfer(alloc, new_slot, old_slot);
121121
}
122-
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
122+
123123
// This overload returns true_type for the trait below.
124124
// The conditional_t is to make the enabler type dependent.
125125
template <class Alloc,
@@ -135,7 +135,6 @@ struct common_policy_traits {
135135
static_cast<const void*>(&element(old_slot)), sizeof(value_type));
136136
return {};
137137
}
138-
#endif
139138

140139
template <class Alloc>
141140
static void transfer_impl(Alloc* alloc, slot_type* new_slot,

absl/container/internal/container_memory.h

-8
Original file line numberDiff line numberDiff line change
@@ -374,19 +374,13 @@ struct map_slot_policy {
374374
return slot->value;
375375
}
376376

377-
// When C++17 is available, we can use std::launder to provide mutable
378-
// access to the key for use in node handle.
379-
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
380377
static K& mutable_key(slot_type* slot) {
381378
// Still check for kMutableKeys so that we can avoid calling std::launder
382379
// unless necessary because it can interfere with optimizations.
383380
return kMutableKeys::value ? slot->key
384381
: *std::launder(const_cast<K*>(
385382
std::addressof(slot->value.first)));
386383
}
387-
#else // !(defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606)
388-
static const K& mutable_key(slot_type* slot) { return key(slot); }
389-
#endif
390384

391385
static const K& key(const slot_type* slot) {
392386
return kMutableKeys::value ? slot->key : slot->value.first;
@@ -443,15 +437,13 @@ struct map_slot_policy {
443437
typename absl::is_trivially_relocatable<value_type>::type();
444438

445439
emplace(new_slot);
446-
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
447440
if (is_relocatable) {
448441
// TODO(b/247130232,b/251814870): remove casts after fixing warnings.
449442
std::memcpy(static_cast<void*>(std::launder(&new_slot->value)),
450443
static_cast<const void*>(&old_slot->value),
451444
sizeof(value_type));
452445
return is_relocatable;
453446
}
454-
#endif
455447

456448
if (kMutableKeys::value) {
457449
absl::allocator_traits<Allocator>::construct(

absl/container/internal/hash_policy_traits.h

-4
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,12 @@ struct hash_policy_traits : common_policy_traits<Policy> {
3636

3737
private:
3838
struct ReturnKey {
39-
// When C++17 is available, we can use std::launder to provide mutable
40-
// access to the key for use in node handle.
41-
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
4239
template <class Key,
4340
absl::enable_if_t<std::is_lvalue_reference<Key>::value, int> = 0>
4441
static key_type& Impl(Key&& k, int) {
4542
return *std::launder(
4643
const_cast<key_type*>(std::addressof(std::forward<Key>(k))));
4744
}
48-
#endif
4945

5046
template <class Key>
5147
static Key Impl(Key&& k, char) {

absl/container/node_hash_map_test.cc

-3
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,6 @@ TEST(NodeHashMap, CForEachMutate) {
322322
}
323323
}
324324

325-
// This test requires std::launder for mutable key access in node handles.
326-
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
327325
TEST(NodeHashMap, NodeHandleMutableKeyAccess) {
328326
node_hash_map<std::string, std::string> map;
329327

@@ -335,7 +333,6 @@ TEST(NodeHashMap, NodeHandleMutableKeyAccess) {
335333

336334
EXPECT_THAT(map, testing::ElementsAre(Pair("key", "mapped")));
337335
}
338-
#endif
339336

340337
TEST(NodeHashMap, RecursiveTypeCompiles) {
341338
struct RecursiveType {

0 commit comments

Comments
 (0)