Make `absl::bit_cast` `constexpr` for MSVC >= 16.10 and enable `ABSL_HAVE_CONSTANT_EVALUATED` for MSVC >= 16.5. These builtins actually exist in MSVC, but AFAIK they cannot be detected with `__has_builtin`. This is really only useful when targeting C++17. When targeting C++20 the proper `std::bit_cast` and `std::is_constant_evaluated` are used. Once Abseil raises its minimum to C++20 these "hacks" can be removed. PiperOrigin-RevId: 1030311488
diff --git a/absl/base/casts.h b/absl/base/casts.h index 17cc0b9..8f37980 100644 --- a/absl/base/casts.h +++ b/absl/base/casts.h
@@ -178,23 +178,30 @@ std::enable_if_t<sizeof(Dest) == sizeof(Source) && std::is_trivially_copyable_v<Source> && std::is_trivially_copyable_v<Dest> -#if !ABSL_HAVE_BUILTIN(__builtin_bit_cast) +#if !(ABSL_HAVE_BUILTIN(__builtin_bit_cast) || \ + (defined(_MSC_VER) && _MSC_VER >= 1929)) && std::is_default_constructible_v<Dest> -#endif // !ABSL_HAVE_BUILTIN(__builtin_bit_cast) +#endif // !(ABSL_HAVE_BUILTIN(__builtin_bit_cast) || (defined(_MSC_VER) && + // _MSC_VER >= 1929)) , int> = 0> -#if ABSL_HAVE_BUILTIN(__builtin_bit_cast) +#if ABSL_HAVE_BUILTIN(__builtin_bit_cast) || \ + (defined(_MSC_VER) && \ + _MSC_VER >= \ + 1929) // Introduced in 1927 with bugs that were addressed with 1929. inline constexpr Dest bit_cast(const Source& source) { return __builtin_bit_cast(Dest, source); } -#else // ABSL_HAVE_BUILTIN(__builtin_bit_cast) +#else // ABSL_HAVE_BUILTIN(__builtin_bit_cast) || (defined(_MSC_VER) && + // _MSC_VER >= 1929) inline Dest bit_cast(const Source& source) { Dest dest; memcpy(static_cast<void*>(std::addressof(dest)), static_cast<const void*>(std::addressof(source)), sizeof(dest)); return dest; } -#endif // ABSL_HAVE_BUILTIN(__builtin_bit_cast) +#endif // ABSL_HAVE_BUILTIN(__builtin_bit_cast) || (defined(_MSC_VER) && + // _MSC_VER >= 1929) #endif // defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
diff --git a/absl/base/config.h b/absl/base/config.h index 4761f25..3cfec58 100644 --- a/absl/base/config.h +++ b/absl/base/config.h
@@ -770,7 +770,8 @@ #endif #ifdef __cpp_lib_is_constant_evaluated #define ABSL_HAVE_CONSTANT_EVALUATED 1 -#elif ABSL_HAVE_BUILTIN(__builtin_is_constant_evaluated) +#elif ABSL_HAVE_BUILTIN(__builtin_is_constant_evaluated) || \ + (defined(_MSC_VER) && _MSC_VER >= 1925) #define ABSL_HAVE_CONSTANT_EVALUATED 1 #endif
diff --git a/absl/meta/type_traits.h b/absl/meta/type_traits.h index 330d61f..5ddff5e 100644 --- a/absl/meta/type_traits.h +++ b/absl/meta/type_traits.h
@@ -468,7 +468,8 @@ constexpr bool is_constant_evaluated() noexcept { #ifdef __cpp_lib_is_constant_evaluated return std::is_constant_evaluated(); -#elif ABSL_HAVE_BUILTIN(__builtin_is_constant_evaluated) +#elif ABSL_HAVE_BUILTIN(__builtin_is_constant_evaluated) || \ + (defined(_MSC_VER) && _MSC_VER >= 1925) return __builtin_is_constant_evaluated(); #endif }