From c3216c182201d001e0ba17515fc8e4fc6d8c2e88 Mon Sep 17 00:00:00 2001 From: David Hunter Date: Sun, 28 Sep 2025 15:59:13 -0600 Subject: [PATCH] Guard includes of standard library headers so users can use import std --- include/gsl/algorithm | 10 +++++++--- include/gsl/assert | 6 +++++- include/gsl/byte | 14 +++++++++++--- include/gsl/narrow | 8 +++++++- include/gsl/pointers | 22 +++++++++++++--------- include/gsl/span | 14 +++++++++----- include/gsl/span_ext | 16 ++++++++++------ include/gsl/util | 22 ++++++++++++++-------- include/gsl/zstring | 6 +++++- 9 files changed, 81 insertions(+), 37 deletions(-) diff --git a/include/gsl/algorithm b/include/gsl/algorithm index c67f35abd..8ba677d5c 100644 --- a/include/gsl/algorithm +++ b/include/gsl/algorithm @@ -20,9 +20,13 @@ #include "./assert" // for Expects #include "./span" // for dynamic_extent, span -#include // for copy_n -#include // for ptrdiff_t -#include // for is_assignable +#if defined(GSL_BUILD_USING_STD_MODULE) + import std; +#else + #include // for copy_n + #include // for ptrdiff_t + #include // for is_assignable +#endif #ifdef _MSC_VER #pragma warning(push) diff --git a/include/gsl/assert b/include/gsl/assert index d3e6ccdd5..25aef7887 100644 --- a/include/gsl/assert +++ b/include/gsl/assert @@ -36,7 +36,11 @@ #else // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && // !_HAS_EXCEPTIONS)) -#include +#if defined(GSL_BUILD_USING_STD_MODULE) + import std; +#else + #include +#endif #endif // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && // !_HAS_EXCEPTIONS)) diff --git a/include/gsl/byte b/include/gsl/byte index fe9b28662..c220e0cc0 100644 --- a/include/gsl/byte +++ b/include/gsl/byte @@ -19,7 +19,11 @@ #include "./util" // for GSL_DEPRECATED -#include +#if defined(GSL_BUILD_USING_STD_MODULE) + import std; +#else + #include +#endif #ifdef _MSC_VER @@ -45,7 +49,9 @@ #else // _MSC_VER #ifndef GSL_USE_STD_BYTE -#include /* __cpp_lib_byte */ +#if !defined(GSL_BUILD_USING_STD_MODULE) + #include /* __cpp_lib_byte */ +#endif // this tests if we are under GCC or Clang with enough -std=c++1z power to get us std::byte // also check if libc++ version is sufficient (> 5.0) or libstdc++ actually contains std::byte #if defined(__cplusplus) && (__cplusplus >= 201703L) && \ @@ -75,7 +81,9 @@ #endif // defined __clang__ || defined __GNUC__ #if GSL_USE_STD_BYTE -#include +#if !defined(GSL_BUILD_USING_STD_MODULE) + #include +#endif #endif namespace gsl diff --git a/include/gsl/narrow b/include/gsl/narrow index bee29a1a8..b6e753b6b 100644 --- a/include/gsl/narrow +++ b/include/gsl/narrow @@ -18,7 +18,13 @@ #define GSL_NARROW_H #include "./assert" // for GSL_SUPPRESS #include "./util" // for narrow_cast -#include // for std::exception + +#if defined(GSL_BUILD_USING_STD_MODULE) + import std; +#else + #include // for std::exception +#endif + namespace gsl { struct narrowing_error : public std::exception diff --git a/include/gsl/pointers b/include/gsl/pointers index 5e69f589e..a196af8a2 100644 --- a/include/gsl/pointers +++ b/include/gsl/pointers @@ -20,15 +20,19 @@ #include "./assert" // for Ensures, Expects #include "./util" // for GSL_DEPRECATED -#include // for ptrdiff_t, nullptr_t, size_t -#include // for less, greater -#include // for shared_ptr, unique_ptr, hash -#include // for enable_if_t, is_convertible, is_assignable -#include // for declval, forward - -#if !defined(GSL_NO_IOSTREAMS) -#include // for ostream -#endif // !defined(GSL_NO_IOSTREAMS) +#if defined(GSL_BUILD_USING_STD_MODULE) + import std; +#else + #include // for ptrdiff_t, nullptr_t, size_t + #include // for less, greater + #include // for shared_ptr, unique_ptr, hash + #include // for enable_if_t, is_convertible, is_assignable + #include // for declval, forward + + #if !defined(GSL_NO_IOSTREAMS) + #include // for ostream + #endif // !defined(GSL_NO_IOSTREAMS) +#endif namespace gsl { diff --git a/include/gsl/span b/include/gsl/span index cef8a7743..8e23e77eb 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -22,11 +22,15 @@ #include "./span_ext" // for span specialization of gsl::at and other span-related extensions #include "./util" // for narrow_cast -#include // for array -#include // for ptrdiff_t, size_t, nullptr_t -#include // for reverse_iterator, distance, random_access_... -#include // for pointer_traits -#include // for enable_if_t, declval, is_convertible, inte... +#if defined(GSL_BUILD_USING_STD_MODULE) + import std; +#else + #include // for array + #include // for ptrdiff_t, size_t, nullptr_t + #include // for reverse_iterator, distance, random_access_... + #include // for pointer_traits + #include // for enable_if_t, declval, is_convertible, inte... +#endif #if defined(__has_include) && __has_include() #include diff --git a/include/gsl/span_ext b/include/gsl/span_ext index 8b2c2c948..ef9d07854 100644 --- a/include/gsl/span_ext +++ b/include/gsl/span_ext @@ -30,12 +30,16 @@ #include "./assert" // GSL_KERNEL_MODE #include "./util" // for narrow_cast, narrow -#include // for ptrdiff_t, size_t -#include - -#ifndef GSL_KERNEL_MODE -#include // for lexicographical_compare -#endif // GSL_KERNEL_MODE +#if defined(GSL_BUILD_USING_STD_MODULE) + import std; +#else + #include // for ptrdiff_t, size_t + #include + + #ifndef GSL_KERNEL_MODE + #include // for lexicographical_compare + #endif // GSL_KERNEL_MODE +#endif namespace gsl { diff --git a/include/gsl/util b/include/gsl/util index 7a3caed48..f34511339 100644 --- a/include/gsl/util +++ b/include/gsl/util @@ -19,17 +19,23 @@ #include "./assert" // for Expects -#include -#include // for ptrdiff_t, size_t -#include // for numeric_limits -#include // for initializer_list -#include // for is_signed, integral_constant -#include // for exchange, forward +#if defined(GSL_BUILD_USING_STD_MODULE) + import std; +#else + #include + #include // for ptrdiff_t, size_t + #include // for numeric_limits + #include // for initializer_list + #include // for is_signed, integral_constant + #include // for exchange, forward +#endif #if defined(__has_include) && __has_include() -#include + #include #if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L -#include +#if !defined(GSL_BUILD_USING_STD_MODULE) + #include +#endif #endif // __cpp_lib_span >= 202002L #endif //__has_include() diff --git a/include/gsl/zstring b/include/gsl/zstring index f00aa055a..a66622f71 100644 --- a/include/gsl/zstring +++ b/include/gsl/zstring @@ -19,7 +19,11 @@ #include "./span_ext" // for dynamic_extent -#include // for size_t, nullptr_t +#if defined(GSL_BUILD_USING_STD_MODULE) + import std; +#else + #include // for size_t, nullptr_t +#endif namespace gsl {