MakeString
Generally speaking, converting things to strings is quite easy. The annoying thing, however, is that it’s not universal:
Numeric types can call std::to_string
Chars can be used as arguments in a string constructor
string_views must be explicitly constructed into strings (due to copy semantics)
Booleans can cast to ints
In addition, converting vectors into strings is not natively supported.
MakeString is a template function interface to provide a standard, unified way to convert entities into strings.
-
template<typename T>
inline std::string MakeString(T obj) Converts the object into a string.
If the object is a vector, the default delimiter - “,” - is used.
- Parameters:
obj – An object (usually a numeric, boolean or vector) to be converted
- Returns:
A string representation
Internal Functions
-
template<typename T, typename = void>
struct MakeStringStruct Internal interface for MakeString.
As with convert(), we use
typename, an internal struct and SFINAE to enforce type behaviour and allow vector partial specialisation. Default struct only applies to numeric types. Overloads handle the others
Specialisations
-
template<>
struct MakeStringStruct<bool, void> Specialization for
bool- Param value:
A boolean true or false
- Return:
The string ‘true’ or ‘false’, as appropriate
Public Static Functions
-
static inline std::string stringify(bool value)
-
template<>
struct MakeStringStruct<char, void> Specialization for
charConverting chars to strings is surprisingly unintuitive. This makes it easier.
Public Static Functions
-
static inline std::string stringify(char value)
-
static inline std::string stringify(char value)