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

Public Static Functions

template<typename U = T, typename = std::enable_if_t<std::is_arithmetic_v<U>>>
static inline std::string stringify(const U &value)

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 char

Converting chars to strings is surprisingly unintuitive. This makes it easier.

Public Static Functions

static inline std::string stringify(char value)
template<>
struct MakeStringStruct<std::string, void>

Specialization for std::string

This exists for performance reasons more than anything else - it’s quicker than the streaming used for numerics.

Public Static Functions

static inline std::string stringify(const std::string &value)
template<>
struct MakeStringStruct<std::string_view, void>

Specialization for std::string_view

Public Static Functions

static inline std::string stringify(const std::string_view &value)

Vector Strings

template<typename T_Inner>
struct MakeStringStruct<std::vector<T_Inner>, void>

Public Static Functions

static inline std::string stringify(const std::vector<T_Inner> &vec)

Specialization for std::vector<T_Inner>

Calls with default delimiter

static inline std::string stringify(const std::vector<T_Inner> &vec, std::string_view delimiter_str)

Specialization for std::vector<T_Inner>

Calls with custom delimiter