YouThe problem is that both the if and else branch will be evaluated at compile-time; you can use Constexpr IfConstexpr If (since C++17) as.
If the value is
true, then statement-false is discarded (if present), otherwise, statement-true is discarded.
template<typename T> string to_str(const T& t){ if constexpr (std::is_integral<T>::value){ return to_str1(t); }else{ return to_str2(t); } } Or overload to_str aswith the help of SFINAE.
// for integral-numbers template <typename T> typename std::enable_if_t<stdenable_if<std::is_integral<T>::value, std::string>::type to_str(T n){ return std::to_string(n); } // for non-integral-numbers template <typename T> typename std::enable_if_t<enable_if<!std::is_integral<T>::value, std::string>::type to_str(const T& v){ std::stringstream ss; ss << v; return ss.str(); } // if T is string just return std::string to_str(const std::string& v) { return v; }