Skip to content

Commit

Permalink
Fix as<std::string> based on JohelEGP feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
filipsajdak committed Oct 2, 2023
1 parent 978d198 commit 426e833
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -928,12 +928,13 @@ inline auto to_string(std::same_as<std::any> auto const&) -> std::string
return "std::any";
}

inline auto to_string(bool b) -> std::string
inline auto to_string(std::same_as<bool> auto b) -> std::string
{
return b ? "true" : "false";
}

template<typename T>
requires (!std::same_as<T, bool>)
inline auto to_string(T&& t) -> std::string
requires requires { std::to_string(t); }
{
Expand All @@ -945,7 +946,8 @@ inline auto to_string(char const& t) -> std::string
return std::string{t};
}

inline auto to_string(char const* s) -> std::string
template <std::same_as<char const*> X>
inline auto to_string(X& s) -> std::string
{
return std::string{s};
}
Expand Down Expand Up @@ -1037,19 +1039,13 @@ inline auto to_string(auto&& value, std::string_view) -> std::string
template <typename T>
concept has_to_string_overload = requires{ static_cast<std::string (*)(T const&)>(&to_string); };

template< std::same_as<std::string> C, non_string X>
template< std::same_as<std::string> C, typename X>
requires has_to_string_overload<X>
auto as( X&& x) -> C
{
return cpp2::to_string(CPP2_FORWARD(x));
}

template< std::same_as<std::string> C>
auto as(bool x) -> C
{
return cpp2::to_string(x);
}

template< std::same_as<std::string> C, std::size_t N>
auto as(const char (&x)[N]) -> C
{
Expand Down

0 comments on commit 426e833

Please sign in to comment.