Skip to content

Commit 65e56ab

Browse files
committed
fix: printing of temporaries
1 parent 76dde7f commit 65e56ab

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

include/mimic++/Printer.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,20 +285,20 @@ namespace mimicpp::detail
285285
template <print_iterator OutIter, format::detail::formattable<CharT> T>
286286
OutIter print(
287287
OutIter out,
288-
T& value,
288+
T&& value,
289289
[[maybe_unused]] const priority_tag<1>
290290
)
291291
{
292292
return format::format_to(
293293
std::move(out),
294294
"{}",
295-
value);
295+
std::forward<T>(value));
296296
}
297297

298298
template <print_iterator OutIter>
299299
OutIter print(
300300
OutIter out,
301-
auto&,
301+
auto&&,
302302
[[maybe_unused]] const priority_tag<0>
303303
)
304304
{

test/unit-tests/Printer.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <catch2/matchers/catch_matchers.hpp>
1111
#include <catch2/matchers/catch_matchers_string.hpp>
1212

13+
#include <ranges>
1314
#include <sstream>
1415

1516
using namespace mimicpp;
@@ -384,6 +385,19 @@ TEST_CASE(
384385
std::move(stream).str(),
385386
Catch::Matchers::Equals("{ {?}, {?}, {?} }"));
386387
}
388+
389+
SECTION("Views are supported.")
390+
{
391+
const std::vector vec{42, 1337};
392+
393+
print(
394+
std::ostreambuf_iterator{stream},
395+
vec
396+
| std::views::transform([](const auto v) { return 2 * v; }));
397+
REQUIRE_THAT(
398+
std::move(stream).str(),
399+
Catch::Matchers::Equals("{ 84, 2674 }"));
400+
}
387401
}
388402

389403
SECTION("std::source_location has specialized printer.")
@@ -405,6 +419,30 @@ TEST_CASE(
405419
}
406420
}
407421

422+
TEST_CASE(
423+
"print supports printing of temporaries.",
424+
"[print]"
425+
)
426+
{
427+
SECTION("Something printable.")
428+
{
429+
StringStreamT stream{};
430+
print(std::ostreambuf_iterator{stream}, 42);
431+
REQUIRE_THAT(
432+
std::move(stream).str(),
433+
Catch::Matchers::Equals("42"));
434+
}
435+
436+
SECTION("Something non-printable.")
437+
{
438+
StringStreamT stream{};
439+
print(std::ostreambuf_iterator{stream}, NonPrintable{});
440+
REQUIRE_THAT(
441+
std::move(stream).str(),
442+
Catch::Matchers::Equals("{?}"));
443+
}
444+
}
445+
408446
TEST_CASE(
409447
"ValueCategory is formattable.",
410448
"[print]"

0 commit comments

Comments
 (0)