Skip to content

Commit

Permalink
deprecate _format UDL in code using FMT_DEPRECATED
Browse files Browse the repository at this point in the history
  • Loading branch information
alexezeder committed Dec 11, 2021
1 parent 3a951a6 commit fe302bf
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3056,7 +3056,7 @@ constexpr auto operator"" _a(const char* s, size_t) -> detail::udl_arg<char> {
using namespace fmt::literals;
std::string message = "The answer is {}"_format(42);
*/
constexpr auto operator"" _format(const char* s, size_t n)
FMT_DEPRECATED constexpr auto operator"" _format(const char* s, size_t n)
-> detail::udl_formatter<char> {
return {{s, n}};
}
Expand Down
5 changes: 3 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ endfunction()
# Adds a test.
# Usage: add_fmt_test(name srcs...)
function(add_fmt_test name)
cmake_parse_arguments(ADD_FMT_TEST "HEADER_ONLY;MODULE" "" "" ${ARGN})
cmake_parse_arguments(ADD_FMT_TEST "HEADER_ONLY;MODULE;DEPRECATED" "" "" ${ARGN})

set(sources ${name}.cc ${ADD_FMT_TEST_UNPARSED_ARGUMENTS})
if (ADD_FMT_TEST_HEADER_ONLY)
Expand All @@ -63,7 +63,7 @@ function(add_fmt_test name)
if (FMT_PEDANTIC)
target_compile_options(${name} PRIVATE ${PEDANTIC_COMPILE_FLAGS})
endif ()
if (FMT_WERROR)
if (FMT_WERROR AND NOT ADD_FMT_TEST_DEPRECATED)
target_compile_options(${name} PRIVATE ${WERROR_FLAG})
endif ()
add_test(NAME ${name} COMMAND ${name})
Expand All @@ -79,6 +79,7 @@ add_fmt_test(format-test mock-allocator.h)
if (MSVC)
target_compile_options(format-test PRIVATE /bigobj)
endif ()
add_fmt_test(format-test-deprecated DEPRECATED)
if (NOT (MSVC AND BUILD_SHARED_LIBS))
add_fmt_test(format-impl-test HEADER_ONLY header-only-test.cc)
endif ()
Expand Down
42 changes: 42 additions & 0 deletions test/format-test-deprecated.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Formatting library for C++ - formatting library tests
//
// Copyright (c) 2012 - present, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.

// clang-format off
#include "fmt/format.h"
// clang-format on

#include "gmock/gmock.h"
#include "util.h"

FMT_BEGIN_NAMESPACE
template <> struct formatter<date> {
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
auto it = ctx.begin();
if (it != ctx.end() && *it == 'd') ++it;
return it;
}

auto format(const date& d, format_context& ctx) -> decltype(ctx.out()) {
format_to(ctx.out(), "{}-{}-{}", d.year(), d.month(), d.day());
return ctx.out();
}
};
FMT_END_NAMESPACE

#if FMT_USE_USER_DEFINED_LITERALS

using namespace fmt::literals;

TEST(format_test, format_udl) {
EXPECT_EQ("{}c{}"_format("ab", 1), fmt::format("{}c{}", "ab", 1));
EXPECT_EQ("foo"_format(), "foo");
EXPECT_EQ("{0:10}"_format(42), " 42");
EXPECT_EQ("{}"_format(date(2015, 10, 21)), "2015-10-21");
}
#endif // FMT_USE_USER_DEFINED_LITERALS

7 changes: 0 additions & 7 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1761,13 +1761,6 @@ TEST(format_test, custom_format_compile_time_string) {

using namespace fmt::literals;

TEST(format_test, format_udl) {
EXPECT_EQ("{}c{}"_format("ab", 1), fmt::format("{}c{}", "ab", 1));
EXPECT_EQ("foo"_format(), "foo");
EXPECT_EQ("{0:10}"_format(42), " 42");
EXPECT_EQ("{}"_format(date(2015, 10, 21)), "2015-10-21");
}

TEST(format_test, named_arg_udl) {
auto udl_a = fmt::format("{first}{second}{first}{third}", "first"_a = "abra",
"second"_a = "cad", "third"_a = 99);
Expand Down

0 comments on commit fe302bf

Please sign in to comment.