函数模板不能部分专业,在一般情况下,它是不使用函数模板专业化是个好主意。达到你想要什么
一种方法是使用一种称为tag分派,基本上包含在提供选择基于一个额外的伪参数的权值过载转发器功能技术:
#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);
}
可能重复: http://stackoverflow.com/questions/12073689/c11-template-function-specialization-for-integer-types – legends2k 2013-10-03 11:53:37