diff options
author | 2023-01-20 07:20:05 -0800 | |
---|---|---|
committer | 2023-01-20 07:21:07 -0800 | |
commit | a0b102c35bae9dea68be21a718c8c7b459e239c8 (patch) | |
tree | 9123391c09f15b2f49ea725636d2b449796dad20 /absl/meta/type_traits_test.cc | |
parent | c611e5ce1de222eb995f5aeabf3fa8cd57ae124b (diff) |
Add absl::is_constant_evaluated for pre-c++20 support
PiperOrigin-RevId: 503437019
Change-Id: I3630fec690f1472130fef21b16dfcd3c5208aa69
Diffstat (limited to 'absl/meta/type_traits_test.cc')
-rw-r--r-- | absl/meta/type_traits_test.cc | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/absl/meta/type_traits_test.cc b/absl/meta/type_traits_test.cc index d08d9ad9..b2a7a67b 100644 --- a/absl/meta/type_traits_test.cc +++ b/absl/meta/type_traits_test.cc @@ -22,6 +22,9 @@ #include "gtest/gtest.h" #include "absl/base/attributes.h" +#include "absl/base/config.h" +#include "absl/time/clock.h" +#include "absl/time/time.h" namespace { @@ -1413,4 +1416,33 @@ TEST(TrivallyRelocatable, Sanity) { EXPECT_TRUE(absl::is_trivially_relocatable<TrivialAbi>::value); } +#ifdef ABSL_HAVE_CONSTANT_EVALUATED + +constexpr int64_t NegateIfConstantEvaluated(int64_t i) { + if (absl::is_constant_evaluated()) { + return -i; + } else { + return i; + } +} + +#endif // ABSL_HAVE_CONSTANT_EVALUATED + +TEST(TrivallyRelocatable, is_constant_evaluated) { +#ifdef ABSL_HAVE_CONSTANT_EVALUATED + constexpr int64_t constant = NegateIfConstantEvaluated(42); + EXPECT_EQ(constant, -42); + + int64_t now = absl::ToUnixSeconds(absl::Now()); + int64_t not_constant = NegateIfConstantEvaluated(now); + EXPECT_EQ(not_constant, now); + + static int64_t const_init = NegateIfConstantEvaluated(42); + EXPECT_EQ(const_init, -42); +#else + GTEST_SKIP() << "absl::is_constant_evaluated is not defined"; +#endif // ABSL_HAVE_CONSTANT_EVALUATED +} + + } // namespace |