From ba82d8775984bcc7643871ab3a33832a9202ef88 Mon Sep 17 00:00:00 2001 From: Filip Sajdak Date: Sat, 1 Oct 2022 18:31:21 +0200 Subject: [PATCH] Fix to_string to call cpp2::to_string Calling std::to_string makes it impossible to use to_string on optional that contains std::string. The solution is to call cpp2::to_string instead of std::to_string. Added examples of use in regression tests with results of execution. --- include/cpp2util.h | 2 +- regression-tests/mixed-string-interpolation.cpp2 | 3 +++ .../test-results/mixed-string-interpolation.cpp | 3 +++ .../test-results/mixed-string-interpolation.cpp2 | 13 +++++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 regression-tests/test-results/mixed-string-interpolation.cpp2 diff --git a/include/cpp2util.h b/include/cpp2util.h index c42d59c59..dbb04e0c3 100644 --- a/include/cpp2util.h +++ b/include/cpp2util.h @@ -748,7 +748,7 @@ inline auto to_string(std::string const& s) -> std::string const& template inline auto to_string(std::optional const& o) -> std::string { if (o.has_value()) { - return std::to_string(o.value()); + return cpp2::to_string(o.value()); } return "(empty)"; } diff --git a/regression-tests/mixed-string-interpolation.cpp2 b/regression-tests/mixed-string-interpolation.cpp2 index 2cc2d64b3..992019982 100644 --- a/regression-tests/mixed-string-interpolation.cpp2 +++ b/regression-tests/mixed-string-interpolation.cpp2 @@ -7,4 +7,7 @@ main: () -> int = { b = 42; std::cout << "a^2 + b = (a * a + b.value())$\n"; + + os: std::optional = "testing"; + std::cout << "os = (os)$\n"; } diff --git a/regression-tests/test-results/mixed-string-interpolation.cpp b/regression-tests/test-results/mixed-string-interpolation.cpp index 3f1e438d7..47537c547 100644 --- a/regression-tests/test-results/mixed-string-interpolation.cpp +++ b/regression-tests/test-results/mixed-string-interpolation.cpp @@ -17,4 +17,7 @@ b = 42; std::cout << "a^2 + b = " + cpp2::to_string(a * a + b.value()) + "\n"; + + std::optional os { "testing" }; + std::cout << "os = " + cpp2::to_string(os) + "\n"; } diff --git a/regression-tests/test-results/mixed-string-interpolation.cpp2 b/regression-tests/test-results/mixed-string-interpolation.cpp2 new file mode 100644 index 000000000..992019982 --- /dev/null +++ b/regression-tests/test-results/mixed-string-interpolation.cpp2 @@ -0,0 +1,13 @@ +#include + +main: () -> int = { + a := 2; + b: std::optional = (); + std::cout << "a = (a)$, b = (b)$\n"; + + b = 42; + std::cout << "a^2 + b = (a * a + b.value())$\n"; + + os: std::optional = "testing"; + std::cout << "os = (os)$\n"; +}