From 014007faf5ef3f00c789340d4f25475b2395205b Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Mon, 9 Sep 2024 17:19:51 +0200 Subject: [PATCH] Add QT_NO_STD_FORMAT_SUPPORT definition to disable std::format support The outcome from the QtCS discussion [0] was that we want to add std::format support directly into the Qt class' header, so that the users do not need to include anything extra to be able to use it. This, however, means that many of Qt headers would include , which itself is quite heavy. As a result, we decided to add a define that allows to disable std::format support, if the users do not need it. This patch introduces QT_NO_STD_FORMAT_SUPPORT as such definition. Users can define it in their projects. It is also defined for Qt for now, because we cannot use std::format inside Qt anyway. Note that we cannot use compile-time QT_FEATURE for that, because Qt itself can be configured with different parameters than the user project. This patch does not add any public documentation for the definition, because it's not yet clear where to provide these docs. However, it adds some comments that would hopefully clarify the situation with the macros. [0]: https://wiki.qt.io/QtCS2024_std::format Fixes: QTBUG-128779 Change-Id: Ie3041966c3a2131e3b47750fbe6831e11b4b74cc Reviewed-by: Thiago Macieira Reviewed-by: Alexey Edelev --- cmake/QtInternalTargets.cmake | 1 + src/corelib/io/qtformat_impl.h | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/cmake/QtInternalTargets.cmake b/cmake/QtInternalTargets.cmake index 50cebbf342a..c53df7e6bfe 100644 --- a/cmake/QtInternalTargets.cmake +++ b/cmake/QtInternalTargets.cmake @@ -156,6 +156,7 @@ qt_internal_add_global_definition(QT_NO_NARROWING_CONVERSIONS_IN_CONNECT) qt_internal_add_global_definition(QT_EXPLICIT_QFILE_CONSTRUCTION_FROM_PATH) qt_internal_add_global_definition(QT_USE_QSTRINGBUILDER SCOPE PLUGIN TOOL MODULE) qt_internal_add_global_definition(QT_NO_FOREACH) +qt_internal_add_global_definition(QT_NO_STD_FORMAT_SUPPORT SCOPE PLUGIN TOOL MODULE) if(WARNINGS_ARE_ERRORS) qt_internal_set_warnings_are_errors_flags(PlatformModuleInternal INTERFACE) diff --git a/src/corelib/io/qtformat_impl.h b/src/corelib/io/qtformat_impl.h index 31031324ae6..648d0a9afe2 100644 --- a/src/corelib/io/qtformat_impl.h +++ b/src/corelib/io/qtformat_impl.h @@ -12,11 +12,22 @@ #include #include +// Users can disable std::format support in their +// projects by using this definition. +#ifndef QT_NO_STD_FORMAT_SUPPORT + #if (defined(__cpp_lib_format) && (__cpp_lib_format >= 202106L)) #include +// If this macro is defined, std::format support is actually available. +// Use it to provide the implementation! +// Note that any out-of-line helper function should not depend on this +// definition, as it should be unconditionally available even in C++17 builds +// to keep BC. #define QT_SUPPORTS_STD_FORMAT 1 #endif // __cpp_lib_format +#endif // QT_NO_STD_FORMAT_SUPPORT + #endif // QTFORMAT_IMPL_H