2013-03-24 133 views
13

如果std::is_integral<>::value为true,那么我试图返回int64_t如何使用std :: is_integral <>选择实现?

否则,我想在对象上调用to_int64t()

我的下面的尝试失败,因为功能模板的部分专业化是不允许的。

CODE

#include <type_traits> 
#include <cstdint> 

template<class T,bool is_integral_type> 
int64_t to_int64t(const T& t) 
{ 
     return t; 
} 

template<class T> 
int64_t to_int64t<T,std::is_integral<T>::value>(const T& t) 
{ 
     return t; 
} 

template<class T> 
int64_t to_int64t<T,!std::is_integral<T>::value>(const T& t) 
{ 
     return t.to_int64t(); 
} 

int main() 
{ 
     int64_t i = 64; 
     auto x = to_int64t(i); 
} 
+0

可能重复: http://stackoverflow.com/questions/12073689/c11-template-function-specialization-for-integer-types – legends2k 2013-10-03 11:53:37

回答

28

函数模板不能部分专业,在一般情况下,它是不使用函数模板专业化是个好主意。达到你想要什么

一种方法是使用一种称为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); 
} 
+0

+1 t Ÿ - 我会试试这个 – kfmfe04 2013-03-24 13:24:44

+0

哦,我的,这是美丽的。 :) – 0x499602D2 2013-03-24 13:27:58

+0

@PeteBecker:感谢您的编辑:) – 2013-03-24 13:33:41

5

你可以用std::enable_if

template<class T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> 
int64_t to_int64t(const T& t) 
{ 
     return t; 
} 

template<class T, typename std::enable_if<!std::is_integral<T>::value, int>::type = 0> 
int64_t to_int64t(const T& t) 
{ 
     return t.to_int64t(); 
} 
相关问题