From 7d1362c7ba96ef13de713869513ca32d79a02931 Mon Sep 17 00:00:00 2001 From: Kent Ross Date: Fri, 14 Oct 2022 16:28:18 -0700 Subject: [PATCH] Do not force C++14 (#10773) * Do not force C++14 Rather than forcing protobuf to always compile in exactly C++14, overriding all settings in .bazelrc and environment variables and commandline options, instead guard the codebase against versions that are too low. This allows for compiling in higher C++ standards, such as those that have std::string_view instead of absl::string_view without generating objects that are incompatible. * Do not guard headers against low C++ versions The code will break below C++14 anyway * Attempt a different >=C++14 guard condition * Steal the >=C++14 guard condition code from absl * Special case C++14 guard for GCC < 5.0 --- build_defs/cpp_opts.bzl | 2 -- src/google/protobuf/stubs/common.h | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/build_defs/cpp_opts.bzl b/build_defs/cpp_opts.bzl index a1d10aa23e..e0b031b092 100644 --- a/build_defs/cpp_opts.bzl +++ b/build_defs/cpp_opts.bzl @@ -14,14 +14,12 @@ COPTS = select({ "/wd4506", # no definition for inline function 'function' "/wd4800", # 'type' : forcing value to bool 'true' or 'false' (performance warning) "/wd4996", # The compiler encountered a deprecated declaration. - "/std:c++14", # Use C++14 ], "//conditions:default": [ "-DHAVE_ZLIB", "-Woverloaded-virtual", "-Wno-sign-compare", "-Werror", - "-std=c++14", # Protobuf requires C++14. ], }) # Android and MSVC builds do not need to link in a separate pthread library. diff --git a/src/google/protobuf/stubs/common.h b/src/google/protobuf/stubs/common.h index cbcee3c926..4d9febcbd6 100644 --- a/src/google/protobuf/stubs/common.h +++ b/src/google/protobuf/stubs/common.h @@ -47,6 +47,24 @@ #include "google/protobuf/stubs/platform_macros.h" #include "google/protobuf/stubs/port.h" +// Enforce C++14 as the minimum. +#if defined(_MSVC_LANG) +#if _MSVC_LANG < 201402L +#error "C++ versions less than C++14 are not supported." +#endif // _MSVC_LANG < 201402L +#elif defined(__cplusplus) +// Special-case GCC < 5.0, as it has a strange __cplusplus value for C++14 +#if defined(__GNUC__) && __GNUC__ < 5 +#if __cplusplus < 201300L +#error "C++ versions less than C++14 are not supported." +#endif // __cplusplus < 201300L +#else // defined(__GNUC__) && __GNUC__ < 5 +#if __cplusplus < 201402L +#error "C++ versions less than C++14 are not supported." +#endif // __cplusplus < 201402L +#endif // defined(__GNUC__) && __GNUC__ < 5 +#endif + #ifndef PROTOBUF_USE_EXCEPTIONS #if defined(_MSC_VER) && defined(_CPPUNWIND) #define PROTOBUF_USE_EXCEPTIONS 1