From b8c77d4041474c3ba2eb0c003c75944136821fa1 Mon Sep 17 00:00:00 2001 From: David Schneider Date: Tue, 15 Mar 2022 17:52:11 -0700 Subject: [PATCH] Make inclusion of header opt-out via macro (#7168) * Allow suppression of including via macro * Combine preprocessor conditions * Make inclusion of header opt-in * Make inclusion of opt-out via macro * Auto-detect C++17+ when defining macro * Enclose macro expression in parentheses * Check value of macro, not whether it's defined * Fix parentheses * Don't define macro with expression using other macros The preprocessor doesn't seem to like it. * Fix parentheses --- include/flatbuffers/stl_emulation.h | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/include/flatbuffers/stl_emulation.h b/include/flatbuffers/stl_emulation.h index 75d13b29c..967f297d6 100644 --- a/include/flatbuffers/stl_emulation.h +++ b/include/flatbuffers/stl_emulation.h @@ -26,16 +26,20 @@ #include #include -// Detect C++17 compatible compiler. -// __cplusplus >= 201703L - a compiler has support of 'static inline' variables. -#if defined(FLATBUFFERS_USE_STD_OPTIONAL) \ - || (defined(__cplusplus) && __cplusplus >= 201703L) \ - || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)) +#ifndef FLATBUFFERS_USE_STD_OPTIONAL + // Detect C++17 compatible compiler. + // __cplusplus >= 201703L - a compiler has support of 'static inline' variables. + #if (defined(__cplusplus) && __cplusplus >= 201703L) \ + || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) + #define FLATBUFFERS_USE_STD_OPTIONAL 1 + #else + #define FLATBUFFERS_USE_STD_OPTIONAL 0 + #endif // (defined(__cplusplus) && __cplusplus >= 201703L) ... +#endif // FLATBUFFERS_USE_STD_OPTIONAL + +#if FLATBUFFERS_USE_STD_OPTIONAL #include - #ifndef FLATBUFFERS_USE_STD_OPTIONAL - #define FLATBUFFERS_USE_STD_OPTIONAL - #endif -#endif // defined(FLATBUFFERS_USE_STD_OPTIONAL) ... +#endif // The __cpp_lib_span is the predefined feature macro. #if defined(FLATBUFFERS_USE_STD_SPAN) @@ -128,7 +132,7 @@ namespace flatbuffers { }; #endif // defined(FLATBUFFERS_TEMPLATES_ALIASES) -#ifdef FLATBUFFERS_USE_STD_OPTIONAL +#if FLATBUFFERS_USE_STD_OPTIONAL template using Optional = std::optional; using nullopt_t = std::nullopt_t;