Skip to content

Commit 0fd2904

Browse files
committed
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.
1 parent 8d5c9b7 commit 0fd2904

File tree

4 files changed

+10
-1
lines changed

4 files changed

+10
-1
lines changed

include/cpp2util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ auto to_string(std::string const& s) -> std::string const&
741741
template<typename T>
742742
auto to_string(std::optional<T> const& o) -> std::string {
743743
if (o.has_value()) {
744-
return std::to_string(o.value());
744+
return cpp2::to_string(o.value());
745745
}
746746
return "(empty)";
747747
}

regression-tests/mixed-string-interpolation.cpp2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ main: () -> int = {
77

88
b = 42;
99
std::cout << "a^2 + b = (a * a + b.value())$\n";
10+
11+
os: std::optional<std::string> = "testing";
12+
std::cout << "os = (os)$\n";
1013
}

regression-tests/test-results/mixed-string-interpolation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717

1818
b = 42;
1919
std::cout << "a^2 + b = " + cpp2::to_string(a * a + b.value()) + "\n";
20+
21+
std::optional<std::string> os { "testing" };
22+
std::cout << "os = " + cpp2::to_string(os) + "\n";
2023
}

regression-tests/test-results/mixed-string-interpolation.cpp2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ main: () -> int = {
77

88
b = 42;
99
std::cout << "a^2 + b = (a * a + b.value())$\n";
10+
11+
os: std::optional<std::string> = "testing";
12+
std::cout << "os = (os)$\n";
1013
}

0 commit comments

Comments
 (0)