From e331b7f432c57d25b9d57763bfc77c933d85ee42 Mon Sep 17 00:00:00 2001 From: Niclas Antti Date: Thu, 18 Apr 2019 20:02:54 +0300 Subject: [PATCH] Add a generic to_string() function. --- maxutils/maxbase/include/maxbase/string.hh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/maxutils/maxbase/include/maxbase/string.hh b/maxutils/maxbase/include/maxbase/string.hh index 9f202496d..cde844cda 100644 --- a/maxutils/maxbase/include/maxbase/string.hh +++ b/maxutils/maxbase/include/maxbase/string.hh @@ -28,6 +28,20 @@ */ const char* mxb_strerror(int error); +/** + * Generate std::string to_string(const T&) for any type T for which there is a + * function std::ostream& operator<<(std::ostream&, const T&) declared. + */ +template::type* = + nullptr> +std::string to_string(const T& t) +{ + std::ostringstream os; + os << t; + return os.str(); +} + namespace maxbase {