Home > Backend Development > C++ > How to Implement a Template Function That Selects Its Implementation Based on the Availability of std::to_string?

How to Implement a Template Function That Selects Its Implementation Based on the Availability of std::to_string?

Linda Hamilton
Release: 2024-11-09 08:25:02
Original
639 people have browsed it

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);
}
Copy after login

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();
}
Copy after login

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();
}
Copy after login

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;
Copy after login

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 { };
Copy after login

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&amp;>(ostringstream() << t).str();
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template