Support Class Library
A set of tools providing classes and utility
toString.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <scl/macros.h>
11 
12 namespace scl{
13  namespace utils{
20  template <class T, class=void>
21  struct ToString;
22 
27  template <class T>
29  META::is_same<T, char>()
30  || META::is_same<T, const char*>()
31  || META::is_same<T, std::string>()
32  >>{
33  std::string operator()(const T& t) const{
34  return std::string{t};
35  }
36  };
37 
42  template <class T>
44  META::defines_std_to_string<T>()
45  >>{
46  std::string operator()(const T& t) const{
47  return std::to_string(t);
48  }
49  };
50 
56  template <class T>
57  std::string toString(const T& obj){
58  static_assert(
59  META::exists<ToString<META::decay_t<T>>>(),
60  "ToString<T> has not been defined"
61  );
62  return ToString<META::decay_t<T>>{}(obj);
63  }
64  }
65 }
Global namespace of the SCL.
Definition: alias.hpp:3
#define META
Definition: macros.h:8
constexpr bool exists()
Determines whether or not the given type is well-formed.
Definition: exists.h:23
std::string toString(const T &obj)
Free function that allows string conversion.
Definition: toString.h:57
typename std::enable_if< B, T >::type enable_if_t
An handy type alias for the result of std::enable_if.
Definition: enable_if.h:12