From c1278f83587fd4084e487451440082c620a4474a Mon Sep 17 00:00:00 2001 From: "petr.dannhofer" Date: Fri, 8 Jul 2022 17:27:18 +0200 Subject: [PATCH] "Fixed" Visual Studio constinit errors both for clang-cl and msvc compilers under c+17 and c+20 standards --- src/google/protobuf/port_def.inc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/google/protobuf/port_def.inc b/src/google/protobuf/port_def.inc index c694a8b7c6..9f82fc6241 100644 --- a/src/google/protobuf/port_def.inc +++ b/src/google/protobuf/port_def.inc @@ -667,14 +667,15 @@ #ifdef PROTOBUF_CONSTINIT #error PROTOBUF_CONSTINIT was previously defined #endif -#if defined(__cpp_constinit) +#if defined(__cpp_constinit) && !defined(_MSC_VER) #define PROTOBUF_CONSTINIT constinit #define PROTOBUF_CONSTEXPR constexpr // Some older Clang versions incorrectly raise an error about // constant-initializing weak default instance pointers. Versions 12.0 and // higher seem to work, except that XCode 12.5.1 shows the error even though it // uses Clang 12.0.5. -#elif __has_cpp_attribute(clang::require_constant_initialization) && \ +// Clang-cl on Windows raises error also. +#elif !defined(_MSC_VER) && __has_cpp_attribute(clang::require_constant_initialization) && \ ((defined(__APPLE__) && __clang_major__ >= 13) || \ (!defined(__APPLE__) && __clang_major__ >= 12)) #define PROTOBUF_CONSTINIT [[clang::require_constant_initialization]] @@ -682,6 +683,10 @@ #elif PROTOBUF_GNUC_MIN(12, 2) #define PROTOBUF_CONSTINIT __constinit #define PROTOBUF_CONSTEXPR constexpr +// MSVC 17 currently seems to raise an error about constant-initialized pointers. +#elif defined(_MSC_VER) && _MSC_VER >= 1930 +#define PROTOBUF_CONSTINIT +#define PROTOBUF_CONSTEXPR constexpr #else #define PROTOBUF_CONSTINIT #define PROTOBUF_CONSTEXPR