diff options
author | Abseil Team <absl-team@google.com> | 2024-07-01 11:33:54 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-07-01 11:34:46 -0700 |
commit | 37ebde53cfcf400ef01b59c80ae3f72039cd90f2 (patch) | |
tree | 1977c5d4878e02cdaa96ed831f775e47bbd58f57 /absl/algorithm/container.h | |
parent | a27662352e9caafc264747562162a8a32ef36cb9 (diff) | |
download | abseil-37ebde53cfcf400ef01b59c80ae3f72039cd90f2.tar.gz abseil-37ebde53cfcf400ef01b59c80ae3f72039cd90f2.tar.bz2 abseil-37ebde53cfcf400ef01b59c80ae3f72039cd90f2.zip |
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<int, 3>());
```
Requires at least C++17 to be constexpr.
PiperOrigin-RevId: 648435141
Change-Id: I8136e351a6dc4c25f06ef895fb449f4f11048480
Diffstat (limited to 'absl/algorithm/container.h')
-rw-r--r-- | absl/algorithm/container.h | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/absl/algorithm/container.h b/absl/algorithm/container.h index 5a025e46..352ac46c 100644 --- a/absl/algorithm/container.h +++ b/absl/algorithm/container.h @@ -52,6 +52,7 @@ #include <vector> #include "absl/algorithm/algorithm.h" +#include "absl/base/config.h" #include "absl/base/macros.h" #include "absl/base/nullability.h" #include "absl/meta/type_traits.h" @@ -93,17 +94,17 @@ using ContainerPointerType = // using std::end; // std::foo(begin(c), end(c)); // becomes -// std::foo(container_algorithm_internal::begin(c), -// container_algorithm_internal::end(c)); +// std::foo(container_algorithm_internal::c_begin(c), +// container_algorithm_internal::c_end(c)); // These are meant for internal use only. template <typename C> -ContainerIter<C> c_begin(C& c) { +ABSL_CONSTEXPR_SINCE_CXX17 ContainerIter<C> c_begin(C& c) { return begin(c); } template <typename C> -ContainerIter<C> c_end(C& c) { +ABSL_CONSTEXPR_SINCE_CXX17 ContainerIter<C> c_end(C& c) { return end(c); } @@ -146,8 +147,9 @@ bool c_linear_search(const C& c, EqualityComparable&& value) { // Container-based version of the <iterator> `std::distance()` function to // return the number of elements within a container. template <typename C> -container_algorithm_internal::ContainerDifferenceType<const C> c_distance( - const C& c) { +ABSL_CONSTEXPR_SINCE_CXX17 + container_algorithm_internal::ContainerDifferenceType<const C> + c_distance(const C& c) { return std::distance(container_algorithm_internal::c_begin(c), container_algorithm_internal::c_end(c)); } |