Skip to main content
added 266 characters in body
Source Link
songyuanyao
  • 173.5k
  • 16
  • 327
  • 432

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; } 

You can use Constexpr If (since C++17) as

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 as

template <typename T> std::enable_if_t<std::is_integral<T>::value, std::string> to_str(T n){ return std::to_string(n); } template <typename T> std::enable_if_t<!std::is_integral<T>::value, std::string> 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; } 

The problem is that both the if and else branch will be evaluated at compile-time; you can use Constexpr If (since C++17).

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 with the help of SFINAE.

// for integral-numbers template <typename T> typename std::enable_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<!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; } 
Source Link
songyuanyao
  • 173.5k
  • 16
  • 327
  • 432

You can use Constexpr If (since C++17) as

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 as

template <typename T> std::enable_if_t<std::is_integral<T>::value, std::string> to_str(T n){ return std::to_string(n); } template <typename T> std::enable_if_t<!std::is_integral<T>::value, std::string> 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; }