Skip to content

Commit

Permalink
"Fixed" Visual Studio constinit errors both for clang-cl and msvc com…
Browse files Browse the repository at this point in the history
…pilers under c+17 and c+20 standards
  • Loading branch information
Eddie-cz authored and acozzette committed Jul 19, 2022
1 parent 3f09570 commit 511c612
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/google/protobuf/port_def.inc
Original file line number Diff line number Diff line change
Expand Up @@ -638,21 +638,26 @@
#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]]
#define PROTOBUF_CONSTEXPR constexpr
#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
Expand Down

0 comments on commit 511c612

Please sign in to comment.