Skip to content

Commit 024f769

Browse files
committed
Fix formatting into std::ostreambuf_iterator using a compiled format
Signed-off-by: Vladislav Shchapov <vladislav@shchapov.ru>
1 parent 01914f0 commit 024f769

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

include/fmt/format.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ FMT_CONSTEXPR FMT_INLINE auto format_decimal(Char* out, UInt value,
12031203
}
12041204

12051205
template <typename Char, typename UInt, typename OutputIt,
1206-
FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>::value)>
1206+
FMT_ENABLE_IF(!std::is_pointer<remove_cvref_t<OutputIt>>::value)>
12071207
FMT_CONSTEXPR auto format_decimal(OutputIt out, UInt value, int num_digits)
12081208
-> OutputIt {
12091209
if (auto ptr = to_pointer<Char>(out, to_unsigned(num_digits))) {

test/compile-test.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "fmt/compile.h"
99

10+
#include <iterator>
11+
#include <list>
1012
#include <type_traits>
1113
#include <vector>
1214

@@ -199,6 +201,21 @@ TEST(compile_test, format_to_n) {
199201
EXPECT_STREQ("2a", buffer);
200202
}
201203

204+
TEST(compile_test, output_iterators) {
205+
std::list<char> out;
206+
fmt::format_to(std::back_inserter(out), FMT_COMPILE("{}"), 42);
207+
EXPECT_EQ("42", std::string(out.begin(), out.end()));
208+
209+
std::stringstream s;
210+
fmt::format_to(std::ostream_iterator<char>(s), FMT_COMPILE("{}"), 42);
211+
EXPECT_EQ("42", s.str());
212+
213+
std::stringstream s2;
214+
fmt::format_to(std::ostreambuf_iterator<char>(s2), FMT_COMPILE("{}.{:06d}"),
215+
42, 43);
216+
EXPECT_EQ("42.000043", s2.str());
217+
}
218+
202219
# if FMT_USE_CONSTEVAL && (!FMT_MSC_VERSION || FMT_MSC_VERSION >= 1940)
203220
TEST(compile_test, constexpr_formatted_size) {
204221
FMT_CONSTEXPR20 size_t size = fmt::formatted_size(FMT_COMPILE("{}"), 42);

test/format-test.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,6 +2102,10 @@ TEST(format_test, output_iterators) {
21022102
std::stringstream s;
21032103
fmt::format_to(std::ostream_iterator<char>(s), "{}", 42);
21042104
EXPECT_EQ("42", s.str());
2105+
2106+
std::stringstream s2;
2107+
fmt::format_to(std::ostreambuf_iterator<char>(s2), "{}.{:06d}", 42, 43);
2108+
EXPECT_EQ("42.000043", s2.str());
21052109
}
21062110

21072111
TEST(format_test, fill_via_appender) {

0 commit comments

Comments
 (0)