From a6f41944899e65b38ec45ee82f6840248f06b471 Mon Sep 17 00:00:00 2001 From: Bernie Innocenti Date: Mon, 30 Jan 2023 21:59:17 -0800 Subject: [PATCH] Fix std::span autodetection (#7805) The current detection method fails on GCC 12.2 with -std=c++20 because the __cpp_lib_span macro is undefined. As per https://en.cppreference.com/w/cpp/utility/feature_test , __cpp_lib_span requires including either or . Since both these headers were added in C++20, checking for C++20 is sufficient (and simpler than using the library feature-test macro). Signed-off-by: Bernie Innocenti Co-authored-by: Derek Bailey --- include/flatbuffers/stl_emulation.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/include/flatbuffers/stl_emulation.h b/include/flatbuffers/stl_emulation.h index 452ddb832..fd3a8cda7 100644 --- a/include/flatbuffers/stl_emulation.h +++ b/include/flatbuffers/stl_emulation.h @@ -41,15 +41,18 @@ #include #endif -// The __cpp_lib_span is the predefined feature macro. -#if defined(FLATBUFFERS_USE_STD_SPAN) - #include -#elif defined(__cpp_lib_span) && defined(__has_include) - #if __has_include() - #include - #include - #define FLATBUFFERS_USE_STD_SPAN +#ifndef FLATBUFFERS_USE_STD_SPAN + // Testing __cpp_lib_span requires including either or , + // both of which were added in C++20. + // See: https://en.cppreference.com/w/cpp/utility/feature_test + #if defined(__cplusplus) && __cplusplus >= 202002L + #define FLATBUFFERS_USE_STD_SPAN 1 #endif +#endif // FLATBUFFERS_USE_STD_SPAN + +#if defined(FLATBUFFERS_USE_STD_SPAN) + #include + #include #else // Disable non-trivial ctors if FLATBUFFERS_SPAN_MINIMAL defined. #if !defined(FLATBUFFERS_TEMPLATES_ALIASES)