Skip to content

Commit f7c4c40

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 bf5998a commit f7c4c40

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

include/cpp2util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ auto to_string(std::string const& s) -> std::string const&
748748
template<typename T>
749749
auto to_string(std::optional<T> const& o) -> std::string {
750750
if (o.has_value()) {
751-
return std::to_string(o.value());
751+
return cpp2::to_string(o.value());
752752
}
753753
return "(empty)";
754754
}

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
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
3+
main: () -> int = {
4+
a := 2;
5+
b: std::optional<int> = ();
6+
std::cout << "a = (a)$, b = (b)$\n";
7+
8+
b = 42;
9+
std::cout << "a^2 + b = (a * a + b.value())$\n";
10+
11+
os: std::optional<std::string> = "testing";
12+
std::cout << "os = (os)$\n";
13+
}

0 commit comments

Comments
 (0)