Home > Article > Backend Development > How to Implement a Template Function That Selects Its Implementation Based on the Availability of std::to_string?
Metaprogramming: Inferring Function Definition Based on Type Availability
In the context of template metaprogramming, it becomes necessary to define templates based on certain criteria. In this particular scenario, the objective is to define a template that selects its implementation based on whether the overloaded to_string function is defined for a given type.
The initial attempt was to use is_arithmetic as the selection criteria:
template<typename T> enable_if_t<is_arithmetic<T>::value, string> stringify(T t){ return to_string(t); }
However, to_string may not be available for non-arithmetic types, leading to the need for an additional template:
template<typename T> enable_if_t<!is_arithmetic<T>::value, string> stringify(T t){ return static_cast<ostringstream&>(ostringstream() << t).str(); }
The challenge arose in defining the template selection criteria for the case where to_string is not available. The following attempt was unsuccessful:
template<typename T> enable_if_t<!decltype(to_string(T{})::value, string> (T t){ return static_cast<ostringstream&>(ostringstream() << t).str(); }
To resolve this issue, we can leverage the void_t type trait introduced by Walter Brown, which allows us to define a type trait that checks for the existence of a function:
template <typename...> using void_t = void;
Using this, we can construct the desired type trait as follows:
template<typename T, typename = void> struct has_to_string : std::false_type { }; template<typename T> struct has_to_string<T, void_t<decltype(std::to_string(std::declval<T>()))> > : std::true_type { };
With this type trait, we can subsequently define the template that selects its implementation based on the availability of to_string:
template<typename T> auto stringify(T t) -> std::enable_if_t<has_to_string<T>::value, std::string> { return std::to_string(t); } template<typename T> auto stringify(T t) -> std::enable_if_t<!has_to_string<T>::value, std::string> { return static_cast<ostringstream&>(ostringstream() << t).str(); }
The above is the detailed content of How to Implement a Template Function That Selects Its Implementation Based on the Availability of std::to_string?. For more information, please follow other related articles on the PHP Chinese website!