From b8d3b37c9efb0e49906bf2af6b61cf37e314db4e Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Mon, 24 Jul 2023 10:33:10 +0100 Subject: [PATCH] Add new macro for standard library header inclusion Newer standard library headers should only be included when the compiler is targetting that standard, otherwise some compilers (like MSVC) will warn that you are using a newer C++ include on an older version. --- include/wx/defs.h | 9 +++++++++ include/wx/string.h | 10 ++++------ interface/wx/defs.h | 11 +++++++++++ src/common/string.cpp | 8 +++----- tests/benchmarks/strings.cpp | 4 ++-- tests/strings/stdstrings.cpp | 2 +- 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/include/wx/defs.h b/include/wx/defs.h index 01e30314cb..ed4ab0770e 100644 --- a/include/wx/defs.h +++ b/include/wx/defs.h @@ -205,6 +205,15 @@ #define wxCHECK_CXX_STD(ver) 0 #endif +/** + * C++ header checks + */ +#if defined(__has_include) + #define wxHAS_CXX17_INCLUDE(header) (wxCHECK_CXX_STD(201703L) && __has_include(header)) +#else + #define wxHAS_CXX17_INCLUDE(header) 0 +#endif + /* ---------------------------------------------------------------------------- */ /* check for native bool type and TRUE/FALSE constants */ /* ---------------------------------------------------------------------------- */ diff --git a/include/wx/string.h b/include/wx/string.h index 6b950af8d1..be59395409 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -46,12 +46,10 @@ #include // Check if C++17 is available -#ifdef __has_include - #if __has_include() - #include - #ifdef __cpp_lib_string_view - #define wxHAS_STD_STRING_VIEW - #endif +#if wxHAS_CXX17_INCLUDE() + #include + #ifdef __cpp_lib_string_view + #define wxHAS_STD_STRING_VIEW #endif #endif diff --git a/interface/wx/defs.h b/interface/wx/defs.h index 2e16245f3e..3cde43201a 100644 --- a/interface/wx/defs.h +++ b/interface/wx/defs.h @@ -1545,6 +1545,17 @@ typedef double wxDouble; */ #define wxCHECK_CXX_STD(stdver) +/** + Returns @true if the compiler is using the C++17 standard and the header @a header exists. + This is designed to guard inclusion of C++17 standard library headers, since MSVC will warn + if a header for a newer C++ standard is included when compiling for an older standard. + + @since 3.3.0 + + @header{wx/defs.h} +*/ +#define wxHAS_CXX17_INCLUDE(header) + /** This macro can be used in a class declaration to disable the generation of default assignment operator. diff --git a/src/common/string.cpp b/src/common/string.cpp index c2ab467f87..9f166a874d 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -1569,11 +1569,9 @@ bool wxString::ToDouble(double *pVal) const // Check if C++17 is available: even though normally it should be // available in any compiler claiming C++17 support, there are actually some // compilers (e.g. gcc 7) that don't have it, so do it in this way instead: -#ifdef __has_include - #if __has_include() - // This should define __cpp_lib_to_chars checked below. - #include - #endif +#if wxHAS_CXX17_INCLUDE() + // This should define __cpp_lib_to_chars checked below. + #include #endif // Now check if the functions we need are present in it (normally they ought diff --git a/tests/benchmarks/strings.cpp b/tests/benchmarks/strings.cpp index 7c03ff7688..a3c48113bc 100644 --- a/tests/benchmarks/strings.cpp +++ b/tests/benchmarks/strings.cpp @@ -663,7 +663,7 @@ BENCHMARK_FUNC(PrintfDouble) return true; } -#if wxCHECK_CXX_STD(201703L) +#if wxHAS_CXX17_INCLUDE() #include @@ -715,4 +715,4 @@ BENCHMARK_FUNC(StdToChars) } #endif // __cpp_lib_to_chars -#endif // C++17 +#endif // wxHAS_CXX17_INCLUDE() diff --git a/tests/strings/stdstrings.cpp b/tests/strings/stdstrings.cpp index a87f6962ef..f3ff37f2ee 100644 --- a/tests/strings/stdstrings.cpp +++ b/tests/strings/stdstrings.cpp @@ -691,4 +691,4 @@ TEST_CASE("StdString::View", "[stdstring]") CHECK( "" == wxString::FromUTF8(strViewInvalidUTF) ); } -#endif +#endif // wxHAS_STD_STRING_VIEW