From 5f5e70e96eaf8a0c3c74aaf4c5bbe39fe7f3dea1 Mon Sep 17 00:00:00 2001 From: Kevin Slattery Date: Wed, 11 May 2022 15:14:41 -0500 Subject: [PATCH 1/2] C++14 build fixes for older gcc #2333 --- example/example.cpp | 7 ++++--- include/spdlog/common.h | 16 ++++++++++++++++ include/spdlog/fmt/bin_to_hex.h | 6 ++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index c379c746..9ccc4519 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -262,15 +262,16 @@ struct my_type : i(i){}; }; -namespace fmt_lib = spdlog::fmt_lib; +FMTLIB_BEGIN_NAMESPACE template<> -struct fmt_lib::formatter : fmt_lib::formatter +struct formatter : formatter { auto format(my_type my, format_context &ctx) -> decltype(ctx.out()) { - return fmt_lib::format_to(ctx.out(), "[my_type i={}]", my.i); + return format_to(ctx.out(), "[my_type i={}]", my.i); } }; +FMTLIB_END_NAMESPACE void user_defined_example() { diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 9ab886f5..ae51c745 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -126,6 +126,13 @@ using sink_ptr = std::shared_ptr; using sinks_init_list = std::initializer_list; using err_handler = std::function; #ifdef SPDLOG_USE_STD_FORMAT +# ifndef FMTLIB_BEGIN_NAMESPACE +# define FMTLIB_BEGIN_NAMESPACE \ + namespace std { +# define FMTLIB_END_NAMESPACE \ + } +# endif + namespace fmt_lib = std; using string_view_t = std::string_view; @@ -146,7 +153,16 @@ template using wformat_string_t = std::wstring_view; # endif # define SPDLOG_BUF_TO_STRING(x) x + #else // use fmt lib instead of std::format + +# ifndef FMTLIB_BEGIN_NAMESPACE +# define FMTLIB_BEGIN_NAMESPACE \ + namespace fmt { +# define FMTLIB_END_NAMESPACE \ + } +# endif + namespace fmt_lib = fmt; using string_view_t = fmt::basic_string_view; diff --git a/include/spdlog/fmt/bin_to_hex.h b/include/spdlog/fmt/bin_to_hex.h index 93e5c28b..2a5d02c9 100644 --- a/include/spdlog/fmt/bin_to_hex.h +++ b/include/spdlog/fmt/bin_to_hex.h @@ -8,8 +8,10 @@ #include #include -#if defined(__has_include) && __has_include() -# include +#if defined(__has_include) +# if __has_include() +# include +# endif #endif #if __cpp_lib_span >= 202002L From d3dee23e6c59624deadd350ca72e51eeb94ddbc5 Mon Sep 17 00:00:00 2001 From: Kevin Slattery Date: Thu, 12 May 2022 18:55:08 -0500 Subject: [PATCH 2/2] Remove new macro, update example with correct way to specify fmt lib namespace when fmt_lib namespace alias cannot be used. --- example/example.cpp | 11 +++++++++-- include/spdlog/common.h | 16 ---------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 9ccc4519..b348afb9 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -262,7 +262,14 @@ struct my_type : i(i){}; }; -FMTLIB_BEGIN_NAMESPACE + +// Using a namespace alias like fmt_lib is not allowed when extending an existing namespace, +// but the correct namespace can still be selected with the SPDLOG_USE_STD_FORMAT macro. +#ifdef SPDLOG_USE_STD_FORMAT + namespace std { +#else + namespace fmt { +#endif template<> struct formatter : formatter { @@ -271,7 +278,7 @@ struct formatter : formatter return format_to(ctx.out(), "[my_type i={}]", my.i); } }; -FMTLIB_END_NAMESPACE +} void user_defined_example() { diff --git a/include/spdlog/common.h b/include/spdlog/common.h index ae51c745..9ab886f5 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -126,13 +126,6 @@ using sink_ptr = std::shared_ptr; using sinks_init_list = std::initializer_list; using err_handler = std::function; #ifdef SPDLOG_USE_STD_FORMAT -# ifndef FMTLIB_BEGIN_NAMESPACE -# define FMTLIB_BEGIN_NAMESPACE \ - namespace std { -# define FMTLIB_END_NAMESPACE \ - } -# endif - namespace fmt_lib = std; using string_view_t = std::string_view; @@ -153,16 +146,7 @@ template using wformat_string_t = std::wstring_view; # endif # define SPDLOG_BUF_TO_STRING(x) x - #else // use fmt lib instead of std::format - -# ifndef FMTLIB_BEGIN_NAMESPACE -# define FMTLIB_BEGIN_NAMESPACE \ - namespace fmt { -# define FMTLIB_END_NAMESPACE \ - } -# endif - namespace fmt_lib = fmt; using string_view_t = fmt::basic_string_view;