函数模板不能进行部分特化,通常情况下,使用函数模板特化并不是一个好主意。
实现您想要的功能的一种方法是使用称为“标签派遣”的技术,这基本上包括提供一个转发函数,该函数根据额外虚拟参数的值选择正确的重载:
#include <type_traits>
#include <cstdint>
template<class T>
int64_t to_int64t( const T& t, std::true_type )
{
return t;
}
template<class T>
int64_t to_int64t( const T& t, std::false_type )
{
return t.to_int64t();
}
template<class T>
int64_t to_int64t( const T& t )
{
return to_int64t(t, std::is_integral<T>());
}
int main()
{
int64_t i = 64;
auto x = to_int64t( i );
}
另一种可能性是使用基于
std::enable_if
的经典 SFINAE 技术。以下是可能的实现方式(请注意,自 C++11 起,函数模板上允许使用默认模板参数):
#include <type_traits>
#include <cstdint>
template<class T, typename std::enable_if<
std::is_integral<T>::value>::type* = nullptr>
int64_t to_int64t( const T& t )
{
return t;
}
template<class T, typename std::enable_if<
!std::is_integral<T>::value>::type* = nullptr>
int64_t to_int64t( const T& t )
{
return t.to_int64t();
}
int main()
{
int64_t i = 64;
auto x = to_int64t( i );
}
另一种可能性,虽然更冗长,是在detail
命名空间中定义辅助类模板(可以部分特化),并提供全局转发器 - 尽管我不会在此用例中使用此技术,但我正在展示它,因为它可能在相关的设计情况下很有用:
#include <type_traits>
#include <cstdint>
namespace detail
{
template<class T, bool = std::is_integral<T>::value>
struct helper { };
template<class T>
struct helper<T, true>
{
static int64_t to_int64t( const T& t )
{
return t;
}
};
template<class T>
struct helper<T, false>
{
static int64_t to_int64t( const T& t )
{
return t.to_int64t();
}
};
}
template<class T>
int64_t to_int64t( const T& t )
{
return detail::helper<T>::to_int64t(t);
}
int main()
{
int64_t i = 64;
auto x = to_int64t( i );
}