Skip to content

Commit

Permalink
Do not force C++14 (#10773)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mumbleskates committed Oct 14, 2022
1 parent fd0b1a5 commit 7d1362c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 0 additions & 2 deletions build_defs/cpp_opts.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions src/google/protobuf/stubs/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7d1362c

Please sign in to comment.