ifruntime.hpp

The following code example is taken from the book
C++17 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2017
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License


#include <string>

template <typename T>
std::string asString(T x)
{
  if (std::is_same_v<T, std::string>) {
    return x;                  // ERROR if no conversion to string
  }
  else if (std::is_numeric_v<T>) {
    return std::to_string(x);  // ERROR if x is not numeric
  }
  else {
    return std::string(x);     // ERROR if no conversion to string
  }
}