Skip to content

Commit 450ee76

Browse files
mkruskal-googlederekmaurocblichmann
authored
Backport: Remove if_constexpr usage for future Abseil compatibility (#20488)
* Use `if constexpr` instead of `absl::utility_internal::IfConstexprElseIfConstexprElse` PiperOrigin-RevId: 728737911 * Remove `absl::if_constexpr` from list of used Abseil targets This is a follow-up on 0ea5ccd, moving to C++'s `if constexpr`. PiperOrigin-RevId: 729136260 * Remove references to unused if_constexpr. This is deleted in an upcoming Abseil release. PiperOrigin-RevId: 731356947 --------- Co-authored-by: Derek Mauro <[email protected]> Co-authored-by: Christian Blichmann <[email protected]>
1 parent 2b9b101 commit 450ee76

File tree

3 files changed

+25
-39
lines changed

3 files changed

+25
-39
lines changed

cmake/abseil-cpp.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ else()
7272
absl::flat_hash_set
7373
absl::function_ref
7474
absl::hash
75-
absl::if_constexpr
7675
absl::layout
7776
absl::log_initialize
7877
absl::log_globals

src/google/protobuf/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,6 @@ cc_library(
457457
"@abseil-cpp//absl/numeric:bits",
458458
"@abseil-cpp//absl/synchronization",
459459
"@abseil-cpp//absl/types:span",
460-
"@abseil-cpp//absl/utility:if_constexpr",
461460
],
462461
)
463462

@@ -584,7 +583,6 @@ cc_library(
584583
"@abseil-cpp//absl/time",
585584
"@abseil-cpp//absl/types:optional",
586585
"@abseil-cpp//absl/types:span",
587-
"@abseil-cpp//absl/utility:if_constexpr",
588586
],
589587
)
590588

src/google/protobuf/arena.h

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ using type_info = ::type_info;
3232
#include "absl/base/optimization.h"
3333
#include "absl/base/prefetch.h"
3434
#include "absl/log/absl_check.h"
35-
#include "absl/utility/internal/if_constexpr.h"
3635
#include "google/protobuf/arena_align.h"
3736
#include "google/protobuf/arena_allocation_policy.h"
3837
#include "google/protobuf/port.h"
@@ -195,41 +194,31 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
195194
// otherwise, returns a heap-allocated object.
196195
template <typename T, typename... Args>
197196
PROTOBUF_NDEBUG_INLINE static T* Create(Arena* arena, Args&&... args) {
198-
return absl::utility_internal::IfConstexprElse<
199-
is_arena_constructable<T>::value>(
200-
// Arena-constructable
201-
[arena](auto&&... args) {
202-
using Type = std::remove_const_t<T>;
203-
#ifdef __cpp_if_constexpr
204-
// DefaultConstruct/CopyConstruct are optimized for messages, which
205-
// are both arena constructible and destructor skippable and they
206-
// assume much. Don't use these functions unless the invariants
207-
// hold.
208-
if constexpr (is_destructor_skippable<T>::value) {
209-
constexpr auto construct_type = GetConstructType<T, Args&&...>();
210-
// We delegate to DefaultConstruct/CopyConstruct where appropriate
211-
// because protobuf generated classes have external templates for
212-
// these functions for code size reasons. When `if constexpr` is not
213-
// available always use the fallback.
214-
if constexpr (construct_type == ConstructType::kDefault) {
215-
return static_cast<Type*>(DefaultConstruct<Type>(arena));
216-
} else if constexpr (construct_type == ConstructType::kCopy) {
217-
return static_cast<Type*>(CopyConstruct<Type>(arena, &args...));
218-
}
219-
}
220-
#endif
221-
return CreateArenaCompatible<Type>(arena,
222-
std::forward<Args>(args)...);
223-
},
224-
// Non arena-constructable
225-
[arena](auto&&... args) {
226-
if (ABSL_PREDICT_FALSE(arena == nullptr)) {
227-
return new T(std::forward<Args>(args)...);
228-
}
229-
return new (arena->AllocateInternal<T>())
230-
T(std::forward<Args>(args)...);
231-
},
232-
std::forward<Args>(args)...);
197+
if constexpr (is_arena_constructable<T>::value) {
198+
using Type = std::remove_const_t<T>;
199+
// DefaultConstruct/CopyConstruct are optimized for messages, which
200+
// are both arena constructible and destructor skippable and they
201+
// assume much. Don't use these functions unless the invariants
202+
// hold.
203+
if constexpr (is_destructor_skippable<T>::value) {
204+
constexpr auto construct_type = GetConstructType<T, Args&&...>();
205+
// We delegate to DefaultConstruct/CopyConstruct where appropriate
206+
// because protobuf generated classes have external templates for
207+
// these functions for code size reasons. When `if constexpr` is not
208+
// available always use the fallback.
209+
if constexpr (construct_type == ConstructType::kDefault) {
210+
return static_cast<Type*>(DefaultConstruct<Type>(arena));
211+
} else if constexpr (construct_type == ConstructType::kCopy) {
212+
return static_cast<Type*>(CopyConstruct<Type>(arena, &args...));
213+
}
214+
}
215+
return CreateArenaCompatible<Type>(arena, std::forward<Args>(args)...);
216+
} else {
217+
if (ABSL_PREDICT_FALSE(arena == nullptr)) {
218+
return new T(std::forward<Args>(args)...);
219+
}
220+
return new (arena->AllocateInternal<T>()) T(std::forward<Args>(args)...);
221+
}
233222
}
234223

235224
// API to delete any objects not on an arena. This can be used to safely

0 commit comments

Comments
 (0)