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.
This commit is contained in:
Ian McInerney 2023-07-24 10:33:10 +01:00
parent 3dfb2a5ac1
commit b8d3b37c9e
6 changed files with 30 additions and 14 deletions

View file

@ -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 */
/* ---------------------------------------------------------------------------- */

View file

@ -46,12 +46,10 @@
#include <utility>
// Check if C++17 <string_view> is available
#ifdef __has_include
#if __has_include(<string_view>)
#include <string_view>
#ifdef __cpp_lib_string_view
#define wxHAS_STD_STRING_VIEW
#endif
#if wxHAS_CXX17_INCLUDE(<string_view>)
#include <string_view>
#ifdef __cpp_lib_string_view
#define wxHAS_STD_STRING_VIEW
#endif
#endif

View file

@ -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.

View file

@ -1569,11 +1569,9 @@ bool wxString::ToDouble(double *pVal) const
// Check if C++17 <charconv> 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(<charconv>)
// This should define __cpp_lib_to_chars checked below.
#include <charconv>
#endif
#if wxHAS_CXX17_INCLUDE(<charconv>)
// This should define __cpp_lib_to_chars checked below.
#include <charconv>
#endif
// Now check if the functions we need are present in it (normally they ought

View file

@ -663,7 +663,7 @@ BENCHMARK_FUNC(PrintfDouble)
return true;
}
#if wxCHECK_CXX_STD(201703L)
#if wxHAS_CXX17_INCLUDE(<charconv>)
#include <charconv>
@ -715,4 +715,4 @@ BENCHMARK_FUNC(StdToChars)
}
#endif // __cpp_lib_to_chars
#endif // C++17
#endif // wxHAS_CXX17_INCLUDE(<charconv>)

View file

@ -691,4 +691,4 @@ TEST_CASE("StdString::View", "[stdstring]")
CHECK( "" == wxString::FromUTF8(strViewInvalidUTF) );
}
#endif
#endif // wxHAS_STD_STRING_VIEW