Skip to content

Commit

Permalink
Fix std::span autodetection
Browse files Browse the repository at this point in the history
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 <version> or <span>.

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 <bernie@codewiz.org>
  • Loading branch information
codewiz committed Jan 30, 2023
1 parent ca71fdf commit ed754dd
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions include/flatbuffers/stl_emulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@
#include <optional>
#endif

// The __cpp_lib_span is the predefined feature macro.
#if defined(FLATBUFFERS_USE_STD_SPAN)
#include <span>
#elif defined(__cpp_lib_span) && defined(__has_include)
#if __has_include(<span>)
#include <array>
#include <span>
#define FLATBUFFERS_USE_STD_SPAN
#ifndef FLATBUFFERS_USE_STD_SPAN
// Testing __cpp_lib_span requires including either <version> or <span>,
// 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 <array>
#include <span>
#else
// Disable non-trivial ctors if FLATBUFFERS_SPAN_MINIMAL defined.
#if !defined(FLATBUFFERS_TEMPLATES_ALIASES)
Expand Down

0 comments on commit ed754dd

Please sign in to comment.