szablony funkcja nie może być częściowo wyspecjalizowane i, w ogóle, to nie jest dobry pomysł, aby korzystać z szablonu funkcji specjalizacji.
Jednym ze sposobów osiągnięcia tego, co chcesz, jest użycie techniki zwanej tag wysyłki, który zasadniczo polega na zapewnieniu funkcji przesyłania dalej, że dobór odpowiedniego przeciążenie w oparciu o wartość dodatkową manekina argumentu:
#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);
}
Inną możliwością jest zastosowanie klasycznej techniki SFINAE opartej na std::enable_if
. To, w jaki sposób mogłaby wyglądać (zauważ, że od C++ 11, argumenty Domyślny szablon na szablonach funkcyjnych są dozwolone):
#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);
}
Jeszcze inna możliwość, choć bardziej gadatliwy, jest zdefiniowanie szablonów klas pomocnika (który może być częściowo specjalistyczne) w detail
nazw i zapewnić globalne spedytora - nie chciałbym używać tej techniki w tym przypadku zastosowania, ale pokazuję, bo to może się przydać w pokrewnych sytuacjach konstrukcja:
#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);
}
możliwym dwóch egzemplarzach: http://stackoverflow.com/questions/12073689/c11-template-function-specialization-for-integer- typeses – legends2k