Skip to content

Commit 92ff88c

Browse files
committed
Refined to_string for variant
1 parent a51e141 commit 92ff88c

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

include/cpp2util.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -800,10 +800,7 @@ inline auto to_string(std::variant<Ts...> const& v) -> std::string
800800
if constexpr (is_any<std::monostate, Ts...>) if (std::get_if<std::monostate>(&v) != nullptr) return "(empty)";
801801

802802
return std::visit([](auto&& arg) -> std::string {
803-
if constexpr (requires { cpp2::to_string(arg); }) {
804-
return cpp2::to_string(arg);
805-
}
806-
return "";
803+
return cpp2::to_string(arg);
807804
}, v);
808805
}
809806

regression-tests/mixed-string-interpolation.cpp2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ main: () -> int = {
4646

4747
vv: std::variant<int, std::string, std::pair<int, double> > = ();
4848
std::cout << "vv = (vv)$\n";
49+
vv = std::make_pair(1,2.3);
50+
std::cout << "vv = (vv)$\n";
4951

5052
std::cout << "custom = (custom)$\n";
5153
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ struct custom_struct_with_no_stringize_customization { } custom;
5656

5757
std::variant<int,std::string,std::pair<int,double>> vv { };
5858
std::cout << "vv = " + cpp2::to_string(vv) + "\n";
59+
vv = std::make_pair(1, 2.3);
60+
std::cout << "vv = " + cpp2::to_string(vv) + "\n";
5961

6062
std::cout << "custom = " + cpp2::to_string(custom) + "\n";
6163
}

0 commit comments

Comments
 (0)