PR #1728: Workaround broken compilation against NDK r25
Imported from GitHub PR https://github.com/abseil/abseil-cpp/pull/1728
When targeting Android NDK r25 (reached EOL now, but still in some use) one gets the following error:
```
$(SOURCE_ROOT)/contrib/restricted/abseil-cpp/absl/time/time.h:1762:37: error: invalid operands to binary expression ('std::strong_ordering' and 'const std::strong_ordering')
if (auto c = lhs_hi <=> rhs_hi; c != std::strong_ordering::equal) {
```
The error indicates the lack of `operator<=>` between two `std::strong_ordering` items.
I believe that this should be controlled by `__cpp_lib_three_way_comparison` (_Three-way comparison (library support)_, see [this article](https://en.cppreference.com/w/cpp/feature_test)), not by `__cpp_impl_three_way_comparison` (which stands for _Three-way comparison (compiler support)_).
Fixes #1725.
The problem was introduced in a27662352e9caafc264747562162a8a32ef36cb9.
Merge 32e1d3d889859ef8a32bf3460349814a2203444c into 69c46839620967c6ffb99b656174d1c544e60a50
Merging this change closes #1728
COPYBARA_INTEGRATE_REVIEW=https://github.com/abseil/abseil-cpp/pull/1728 from georgthegreat:fix-time 32e1d3d889859ef8a32bf3460349814a2203444c
PiperOrigin-RevId: 663723253
Change-Id: I29f7d2b562cc5ad8e11cd46b538ba69acee0f314
diff --git a/absl/time/duration_test.cc b/absl/time/duration_test.cc
index 4c801a8..416bf3c 100644
--- a/absl/time/duration_test.cc
+++ b/absl/time/duration_test.cc
@@ -16,14 +16,19 @@
#include <winsock2.h> // for timeval
#endif
+#include "absl/base/config.h"
+
+// For feature testing and determining which headers can be included.
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+#include <version>
+#endif
+
#include <array>
#include <cfloat>
#include <chrono> // NOLINT(build/c++11)
-
-#ifdef __cpp_impl_three_way_comparison
+#ifdef __cpp_lib_three_way_comparison
#include <compare>
-#endif // __cpp_impl_three_way_comparison
-
+#endif // __cpp_lib_three_way_comparison
#include <cmath>
#include <cstdint>
#include <ctime>
@@ -437,14 +442,14 @@
EXPECT_LT(-inf, inf);
EXPECT_GT(inf, -inf);
-#ifdef __cpp_impl_three_way_comparison
+#ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
EXPECT_EQ(inf <=> inf, std::strong_ordering::equal);
EXPECT_EQ(-inf <=> -inf, std::strong_ordering::equal);
EXPECT_EQ(-inf <=> inf, std::strong_ordering::less);
EXPECT_EQ(inf <=> -inf, std::strong_ordering::greater);
EXPECT_EQ(any_dur <=> inf, std::strong_ordering::less);
EXPECT_EQ(any_dur <=> -inf, std::strong_ordering::greater);
-#endif // __cpp_impl_three_way_comparison
+#endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
}
TEST(Duration, InfinityAddition) {
@@ -511,18 +516,18 @@
absl::Duration almost_neg_inf = sec_min;
EXPECT_LT(-inf, almost_neg_inf);
-#ifdef __cpp_impl_three_way_comparison
+#ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
EXPECT_EQ(-inf <=> almost_neg_inf, std::strong_ordering::less);
EXPECT_EQ(almost_neg_inf <=> -inf, std::strong_ordering::greater);
-#endif // __cpp_impl_three_way_comparison
+#endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
almost_neg_inf -= -absl::Nanoseconds(1);
EXPECT_LT(-inf, almost_neg_inf);
-#ifdef __cpp_impl_three_way_comparison
+#ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
EXPECT_EQ(-inf <=> almost_neg_inf, std::strong_ordering::less);
EXPECT_EQ(almost_neg_inf <=> -inf, std::strong_ordering::greater);
-#endif // __cpp_impl_three_way_comparison
+#endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
// For reference: IEEE 754 behavior
const double dbl_inf = std::numeric_limits<double>::infinity();
@@ -883,7 +888,7 @@
EXPECT_LT(neg_full_range, full_range);
EXPECT_EQ(neg_full_range, -full_range);
-#ifdef __cpp_impl_three_way_comparison
+#ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
EXPECT_EQ(range_future <=> absl::InfiniteDuration(),
std::strong_ordering::less);
EXPECT_EQ(range_past <=> -absl::InfiniteDuration(),
@@ -896,7 +901,7 @@
std::strong_ordering::greater);
EXPECT_EQ(neg_full_range <=> full_range, std::strong_ordering::less);
EXPECT_EQ(neg_full_range <=> -full_range, std::strong_ordering::equal);
-#endif // __cpp_impl_three_way_comparison
+#endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
}
TEST(Duration, RelationalOperators) {
@@ -920,8 +925,7 @@
#undef TEST_REL_OPS
}
-
-#ifdef __cpp_impl_three_way_comparison
+#ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
TEST(Duration, SpaceshipOperators) {
#define TEST_REL_OPS(UNIT) \
@@ -939,7 +943,7 @@
#undef TEST_REL_OPS
}
-#endif // __cpp_impl_three_way_comparison
+#endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
TEST(Duration, Addition) {
#define TEST_ADD_OPS(UNIT) \
diff --git a/absl/time/time.h b/absl/time/time.h
index f133c2d..3fd8d60 100644
--- a/absl/time/time.h
+++ b/absl/time/time.h
@@ -74,13 +74,19 @@
// including 'windows.h' so we are picking the lesser of two evils here.
struct timeval;
#endif
+
+#include "absl/base/config.h"
+
+// For feature testing and determining which headers can be included.
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+#include <version>
+#endif
+
#include <chrono> // NOLINT(build/c++11)
-
-#ifdef __cpp_impl_three_way_comparison
-#include <compare>
-#endif // __cpp_impl_three_way_comparison
-
#include <cmath>
+#ifdef __cpp_lib_three_way_comparison
+#include <compare>
+#endif // __cpp_lib_three_way_comparison
#include <cstdint>
#include <ctime>
#include <limits>
@@ -91,12 +97,16 @@
#include <utility>
#include "absl/base/attributes.h"
-#include "absl/base/config.h"
#include "absl/base/macros.h"
#include "absl/strings/string_view.h"
#include "absl/time/civil_time.h"
#include "absl/time/internal/cctz/include/cctz/time_zone.h"
+#if defined(__cpp_impl_three_way_comparison) && \
+ defined(__cpp_lib_three_way_comparison)
+#define ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON 1
+#endif
+
namespace absl {
ABSL_NAMESPACE_BEGIN
@@ -313,12 +323,12 @@
// Relational Operators
-#ifdef __cpp_impl_three_way_comparison
+#ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
ABSL_ATTRIBUTE_CONST_FUNCTION constexpr std::strong_ordering operator<=>(
Duration lhs, Duration rhs);
-#endif // __cpp_impl_three_way_comparison
+#endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<(Duration lhs,
Duration rhs);
@@ -853,9 +863,9 @@
friend constexpr Time time_internal::FromUnixDuration(Duration d);
friend constexpr Duration time_internal::ToUnixDuration(Time t);
-#ifdef __cpp_impl_three_way_comparison
+#ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
friend constexpr std::strong_ordering operator<=>(Time lhs, Time rhs);
-#endif // __cpp_impl_three_way_comparison
+#endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
friend constexpr bool operator<(Time lhs, Time rhs);
friend constexpr bool operator==(Time lhs, Time rhs);
@@ -868,14 +878,14 @@
};
// Relational Operators
-#ifdef __cpp_impl_three_way_comparison
+#ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
ABSL_ATTRIBUTE_CONST_FUNCTION constexpr std::strong_ordering operator<=>(
Time lhs, Time rhs) {
return lhs.rep_ <=> rhs.rep_;
}
-#endif // __cpp_impl_three_way_comparison
+#endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<(Time lhs, Time rhs) {
return lhs.rep_ < rhs.rep_;
@@ -1752,8 +1762,7 @@
: time_internal::GetRepLo(lhs) < time_internal::GetRepLo(rhs);
}
-
-#ifdef __cpp_impl_three_way_comparison
+#ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
ABSL_ATTRIBUTE_CONST_FUNCTION constexpr std::strong_ordering operator<=>(
Duration lhs, Duration rhs) {
@@ -1769,7 +1778,7 @@
: lhs_lo <=> rhs_lo;
}
-#endif // __cpp_impl_three_way_comparison
+#endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator==(Duration lhs,
Duration rhs) {
diff --git a/absl/time/time_test.cc b/absl/time/time_test.cc
index 71f54d6..69e93e9 100644
--- a/absl/time/time_test.cc
+++ b/absl/time/time_test.cc
@@ -13,25 +13,28 @@
// limitations under the License.
#include "absl/time/time.h"
-
-#include <cstdint>
-#include <ios>
-
#include "absl/time/civil_time.h"
#if defined(_MSC_VER)
#include <winsock2.h> // for timeval
#endif
+#include "absl/base/config.h"
+
+// For feature testing and determining which headers can be included.
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
+#include <version>
+#endif
+
#include <chrono> // NOLINT(build/c++11)
-
-#ifdef __cpp_impl_three_way_comparison
+#ifdef __cpp_lib_three_way_comparison
#include <compare>
-#endif // __cpp_impl_three_way_comparison
-
+#endif // __cpp_lib_three_way_comparison
+#include <cstdint>
#include <cstring>
#include <ctime>
#include <iomanip>
+#include <ios>
#include <limits>
#include <string>
@@ -213,7 +216,7 @@
static_assert(t1 >= t1, "");
static_assert(t3 >= t1, "");
-#ifdef __cpp_impl_three_way_comparison
+#ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
static_assert((t1 <=> t1) == std::strong_ordering::equal, "");
static_assert((t2 <=> t2) == std::strong_ordering::equal, "");
@@ -227,7 +230,7 @@
static_assert((t3 <=> t2) == std::strong_ordering::greater, "");
static_assert((t3 <=> t1) == std::strong_ordering::greater, "");
-#endif // __cpp_impl_three_way_comparison
+#endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
}
TEST(Time, Infinity) {
@@ -239,14 +242,14 @@
static_assert(ipast < ifuture, "");
static_assert(ifuture > ipast, "");
-#ifdef __cpp_impl_three_way_comparison
+#ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
static_assert((ifuture <=> ifuture) == std::strong_ordering::equal, "");
static_assert((ipast <=> ipast) == std::strong_ordering::equal, "");
static_assert((ipast <=> ifuture) == std::strong_ordering::less, "");
static_assert((ifuture <=> ipast) == std::strong_ordering::greater, "");
-#endif // __cpp_impl_three_way_comparison
+#endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
// Arithmetic saturates
EXPECT_EQ(ifuture, ifuture + absl::Seconds(1));
@@ -263,14 +266,14 @@
static_assert(t < ifuture, "");
static_assert(t > ipast, "");
-#ifdef __cpp_impl_three_way_comparison
+#ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
static_assert((t <=> ifuture) == std::strong_ordering::less, "");
static_assert((t <=> ipast) == std::strong_ordering::greater, "");
static_assert((ipast <=> t) == std::strong_ordering::less, "");
static_assert((ifuture <=> t) == std::strong_ordering::greater, "");
-#endif // __cpp_impl_three_way_comparison
+#endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON
EXPECT_EQ(ifuture, t + absl::InfiniteDuration());
EXPECT_EQ(ipast, t - absl::InfiniteDuration());