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.
#include <string>
template <typename T>
std::string asString(T x)
{
if constexpr(std::is_same_v<T, std::string>) {
return x; // statement invalid if no conversion to string
}
else if constexpr(std::is_arithmetic_v<T>) {
return std::to_string(x); // statement invalid if x is not numeric
}
else {
return std::string(x); // statement invalid if no conversion to string
}
}