mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 18:57:34 +02:00
Deprecate the fmt macro
This commit is contained in:
@ -17,7 +17,7 @@ The {fmt} library API consists of the following parts:
|
|||||||
* :ref:`fmt/printf.h <printf-api>`: ``printf`` formatting
|
* :ref:`fmt/printf.h <printf-api>`: ``printf`` formatting
|
||||||
|
|
||||||
All functions and types provided by the library reside in namespace ``fmt`` and
|
All functions and types provided by the library reside in namespace ``fmt`` and
|
||||||
macros have prefix ``FMT_`` or ``fmt``.
|
macros have prefix ``FMT_``.
|
||||||
|
|
||||||
.. _core-api:
|
.. _core-api:
|
||||||
|
|
||||||
@ -98,7 +98,6 @@ Compile-time Format String Checks
|
|||||||
---------------------------------
|
---------------------------------
|
||||||
|
|
||||||
.. doxygendefine:: FMT_STRING
|
.. doxygendefine:: FMT_STRING
|
||||||
.. doxygendefine:: fmt
|
|
||||||
|
|
||||||
Formatting User-defined Types
|
Formatting User-defined Types
|
||||||
-----------------------------
|
-----------------------------
|
||||||
|
@ -3602,6 +3602,22 @@ FMT_CONSTEXPR internal::udl_arg<wchar_t> operator"" _a(const wchar_t* s,
|
|||||||
#endif // FMT_USE_USER_DEFINED_LITERALS
|
#endif // FMT_USE_USER_DEFINED_LITERALS
|
||||||
FMT_END_NAMESPACE
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
|
#define FMT_STRING_IMPL(s, ...) \
|
||||||
|
[] { \
|
||||||
|
struct str : fmt::compile_string { \
|
||||||
|
using char_type = typename std::remove_cv<std::remove_pointer< \
|
||||||
|
typename std::decay<decltype(s)>::type>::type>::type; \
|
||||||
|
__VA_ARGS__ FMT_CONSTEXPR \
|
||||||
|
operator fmt::basic_string_view<char_type>() const { \
|
||||||
|
return {s, sizeof(s) / sizeof(char_type) - 1}; \
|
||||||
|
} \
|
||||||
|
} result; \
|
||||||
|
/* Suppress Qt Creator warning about unused operator. */ \
|
||||||
|
(void)static_cast<fmt::basic_string_view<typename str::char_type>>( \
|
||||||
|
result); \
|
||||||
|
return result; \
|
||||||
|
}()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
\rst
|
\rst
|
||||||
Constructs a compile-time format string.
|
Constructs a compile-time format string.
|
||||||
@ -3612,37 +3628,10 @@ FMT_END_NAMESPACE
|
|||||||
std::string s = format(FMT_STRING("{:d}"), "foo");
|
std::string s = format(FMT_STRING("{:d}"), "foo");
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
#define FMT_STRING(s) \
|
#define FMT_STRING(s) FMT_STRING_IMPL(s, )
|
||||||
[] { \
|
|
||||||
struct str : fmt::compile_string { \
|
|
||||||
using char_type = typename std::remove_cv<std::remove_pointer< \
|
|
||||||
typename std::decay<decltype(s)>::type>::type>::type; \
|
|
||||||
FMT_CONSTEXPR operator fmt::basic_string_view<char_type>() const { \
|
|
||||||
return {s, sizeof(s) / sizeof(char_type) - 1}; \
|
|
||||||
} \
|
|
||||||
} result; \
|
|
||||||
/* Suppress Qt Creator warning about unused operator. */ \
|
|
||||||
(void)static_cast<fmt::basic_string_view<typename str::char_type>>( \
|
|
||||||
result); \
|
|
||||||
return result; \
|
|
||||||
}()
|
|
||||||
|
|
||||||
#if defined(FMT_STRING_ALIAS) && FMT_STRING_ALIAS
|
#if defined(FMT_STRING_ALIAS) && FMT_STRING_ALIAS
|
||||||
/**
|
# define fmt(s) FMT_STRING_IMPL(s, [[deprecated]])
|
||||||
\rst
|
|
||||||
Constructs a compile-time format string. This macro is disabled by default to
|
|
||||||
prevent potential name collisions. To enable it define ``FMT_STRING_ALIAS`` to
|
|
||||||
1 before including ``fmt/format.h``.
|
|
||||||
|
|
||||||
**Example**::
|
|
||||||
|
|
||||||
#define FMT_STRING_ALIAS 1
|
|
||||||
#include <fmt/format.h>
|
|
||||||
// A compile-time error because 'd' is an invalid specifier for strings.
|
|
||||||
std::string s = format(fmt("{:d}"), "foo");
|
|
||||||
\endrst
|
|
||||||
*/
|
|
||||||
# define fmt(s) FMT_STRING(s)
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef FMT_HEADER_ONLY
|
#ifdef FMT_HEADER_ONLY
|
||||||
|
@ -187,8 +187,8 @@ TEST(OStreamTest, Join) {
|
|||||||
|
|
||||||
#if FMT_USE_CONSTEXPR
|
#if FMT_USE_CONSTEXPR
|
||||||
TEST(OStreamTest, ConstexprString) {
|
TEST(OStreamTest, ConstexprString) {
|
||||||
EXPECT_EQ("42", format(fmt("{}"), std::string("42")));
|
EXPECT_EQ("42", format(FMT_STRING("{}"), std::string("42")));
|
||||||
EXPECT_EQ("a string", format(fmt("{0}"), TestString("a string")));
|
EXPECT_EQ("a string", format(FMT_STRING("{0}"), TestString("a string")));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user