From 37ebde53cfcf400ef01b59c80ae3f72039cd90f2 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 1 Jul 2024 11:33:54 -0700 Subject: Make c_begin, c_end, and c_distance conditionally constexpr. This allows them to be used in constexpr expressions, such as the following: ``` constexpr int distance = absl::c_distance(std::array()); ``` Requires at least C++17 to be constexpr. PiperOrigin-RevId: 648435141 Change-Id: I8136e351a6dc4c25f06ef895fb449f4f11048480 --- absl/algorithm/container_test.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'absl/algorithm/container_test.cc') diff --git a/absl/algorithm/container_test.cc b/absl/algorithm/container_test.cc index 7e54e2b2..e5e2bf3d 100644 --- a/absl/algorithm/container_test.cc +++ b/absl/algorithm/container_test.cc @@ -15,6 +15,7 @@ #include "absl/algorithm/container.h" #include +#include #include #include #include @@ -31,6 +32,7 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" #include "absl/base/casts.h" +#include "absl/base/config.h" #include "absl/base/macros.h" #include "absl/memory/memory.h" #include "absl/types/span.h" @@ -1160,4 +1162,13 @@ TEST(MutatingTest, PermutationOperations) { EXPECT_EQ(initial, permuted); } +#if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \ + ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L +TEST(ConstexprTest, Distance) { + // Works at compile time with constexpr containers. + static_assert(absl::c_distance(std::array()) == 3); +} +#endif // defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && + // ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L + } // namespace -- cgit v1.2.3