diff --git a/include/fmt/core.h b/include/fmt/core.h index 55ae22db..6611d5b8 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -140,6 +140,20 @@ # endif #endif +#ifndef FMT_DEPRECATED +# if FMT_HAS_CPP14_ATTRIBUTE(deprecated) || FMT_MSC_VERSION >= 1900 +# define FMT_DEPRECATED [[deprecated]] +# else +# if (defined(__GNUC__) && !defined(__LCC__)) || defined(__clang__) +# define FMT_DEPRECATED __attribute__((deprecated)) +# elif FMT_MSC_VERSION +# define FMT_DEPRECATED __declspec(deprecated) +# else +# define FMT_DEPRECATED /* deprecated */ +# endif +# endif +#endif + // [[noreturn]] is disabled on MSVC and NVCC because of bogus unreachable code // warnings. #if FMT_EXCEPTIONS && FMT_HAS_CPP_ATTRIBUTE(noreturn) && !FMT_MSC_VERSION && \ @@ -1420,9 +1434,9 @@ template struct arg_mapper { template ::value&& std::is_convertible::value && - !has_formatter::value && + !has_format_as::value && !has_formatter::value && !has_fallback_formatter::value)> - FMT_CONSTEXPR FMT_INLINE auto map(const T& val) + FMT_DEPRECATED FMT_CONSTEXPR FMT_INLINE auto map(const T& val) -> decltype(std::declval().map( static_cast>(val))) { return map(static_cast>(val)); diff --git a/include/fmt/format.h b/include/fmt/format.h index 37bca46d..4e146603 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -118,20 +118,6 @@ FMT_END_NAMESPACE # endif #endif -#ifndef FMT_DEPRECATED -# if FMT_HAS_CPP14_ATTRIBUTE(deprecated) || FMT_MSC_VERSION >= 1900 -# define FMT_DEPRECATED [[deprecated]] -# else -# if (defined(__GNUC__) && !defined(__LCC__)) || defined(__clang__) -# define FMT_DEPRECATED __attribute__((deprecated)) -# elif FMT_MSC_VERSION -# define FMT_DEPRECATED __declspec(deprecated) -# else -# define FMT_DEPRECATED /* deprecated */ -# endif -# endif -#endif - #ifndef FMT_USE_USER_DEFINED_LITERALS // EDG based compilers (Intel, NVIDIA, Elbrus, etc), GCC and MSVC support UDLs. # if (FMT_HAS_FEATURE(cxx_user_literals) || FMT_GCC_VERSION >= 407 || \ diff --git a/test/format-test.cc b/test/format-test.cc index 5d6662d3..f6e586f6 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1756,6 +1756,7 @@ TEST(format_test, group_digits_view) { } enum test_enum { foo, bar }; +auto format_as(test_enum e) -> int { return e; } TEST(format_test, join) { using fmt::join; @@ -1902,6 +1903,7 @@ TEST(format_test, formatter_not_specialized) { #if FMT_HAS_FEATURE(cxx_strong_enums) enum big_enum : unsigned long long { big_enum_value = 5000000000ULL }; +auto format_as(big_enum e) -> unsigned long long { return e; } TEST(format_test, strong_enum) { EXPECT_EQ("5000000000", fmt::format("{}", big_enum_value)); @@ -1983,9 +1985,7 @@ TEST(format_test, to_string) { EXPECT_EQ(fmt::to_string(reinterpret_cast(0x1234)), "0x1234"); EXPECT_EQ(fmt::to_string(adl_test::fmt::detail::foo()), "foo"); EXPECT_EQ(fmt::to_string(convertible_to_int()), "42"); - - enum foo : unsigned char { zero }; - EXPECT_EQ(fmt::to_string(zero), "0"); + EXPECT_EQ(fmt::to_string(foo), "0"); #if FMT_USE_FLOAT128 EXPECT_EQ(fmt::to_string(__float128(0.5)), "0.5"); diff --git a/test/ostream-test.cc b/test/ostream-test.cc index f88ae936..a3a51d53 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -54,6 +54,7 @@ auto operator<<(std::ostream& os, streamable_enum) -> std::ostream& { } enum unstreamable_enum {}; +auto format_as(unstreamable_enum e) -> int { return e; } struct empty_test {}; auto operator<<(std::ostream& os, empty_test) -> std::ostream& { diff --git a/test/printf-test.cc b/test/printf-test.cc index 91869c2f..383ffb83 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -504,6 +504,7 @@ TEST(printf_test, pointer) { } enum test_enum { answer = 42 }; +auto format_as(test_enum e) -> int { return e; } TEST(printf_test, enum) { EXPECT_PRINTF("42", "%d", answer); diff --git a/test/ranges-test.cc b/test/ranges-test.cc index 468e4f40..0804996f 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -224,6 +224,7 @@ TEST(ranges_test, range) { } enum test_enum { foo }; +auto format_as(test_enum e) -> int { return e; } TEST(ranges_test, enum_range) { auto v = std::vector{test_enum::foo}; diff --git a/test/xchar-test.cc b/test/xchar-test.cc index 4183b4b8..52dc4ff4 100644 --- a/test/xchar-test.cc +++ b/test/xchar-test.cc @@ -222,6 +222,7 @@ struct formatter : basic_ostream_formatter { } // namespace fmt enum unstreamable_enum {}; +auto format_as(unstreamable_enum e) -> int { return e; } TEST(xchar_test, enum) { EXPECT_EQ(L"streamable_enum", fmt::format(L"{}", streamable_enum()));