模板特化类型

C++的模板及模板特化的几种特化:
方式1特化:
template<class T>
class Compare
{
public:
    static bool IsEqual(const T& lh, const T& rh)
    {
        return lh == rh;
    }
};
template<>
class Compare<float>
{
public:
    static bool IsEqual(const float& lh, const float& rh)
    {
        return abs(lh - rh) < 10e-3;
    }
};
方式2特化:
template <class _Iterator>
struct iterator_traits {
  typedef typename _Iterator::iterator_category iterator_category;
  typedef typename _Iterator::value_type        value_type;
  typedef typename _Iterator::difference_type   difference_type;
  typedef typename _Iterator::pointer           pointer;
  typedef typename _Iterator::reference         reference;
};
// specialize for _Tp*
template <class _Tp>
struct iterator_traits<_Tp*> {
  typedef random_access_iterator_tag iterator_category;
  typedef _Tp                         value_type;
  typedef ptrdiff_t                   difference_type;
  typedef _Tp*                        pointer;
  typedef _Tp&                        reference;
};
// specialize for const _Tp*
template <class _Tp>
struct iterator_traits<const _Tp*> {
  typedef random_access_iterator_tag iterator_category;
  typedef _Tp                         value_type;
  typedef ptrdiff_t                   difference_type;
  typedef const _Tp*                  pointer;
  typedef const _Tp&                  reference;
};
方式3特化
template<class T>
class Compare<vector<T> >
{
public:
    static bool IsEqual(const vector<T>& lh, const vector<T>& rh)
    {
        if(lh.size() != rh.size()) return false;
        else
        {
            for(int i = 0; i < lh.size(); ++i)
            {
                if(lh[i] != rh[i]) return false;
            }
        }
        return true;
    }
};
方式4特化
template<typename T1, typename T2> 
class X 
{}; 
template<typename T> 
class X<T, int> {}; 
以及2、3、4的混合 
template<typename T> 
class X<T, T*> {} 
template<typename T> 
class X<vector<T>, T&> {}; 
方式5特化
template<typename T> 
class Y; 
template<typename R, typename P1, typename P2> 
class Y<R (P1, P2)> {};
方式6特化
#include <iostream>
#include <string>
#include <tuple>
#include <type_traits>
using namespace std;
template <class T, std::size_t N, class... Args>
struct index_of
{
    static constexpr auto value = N;
};
template <class T, std::size_t N, class... Args>
struct index_of<T, N, T, Args...>
{
    static constexpr auto value = N;
};
template <class T, std::size_t N, class U, class... Args>
struct index_of<T, N, U, Args...>
{
    static constexpr auto value = index_of<T, N + 1, Args...>::value;
};
template <class T, std::size_t N>
struct index_of<T, N>
{
    static constexpr auto value = -1;
};
template <class T, class... Args>
T get_element_by_type(const std::tuple<Args...>& t)
{
    std::cout<<index_of<signed char, 0, Args...>::value<<std::endl;
    typename std::enable_if<index_of<signed char, 0, Args...>::value < 0, int>::type a;
    static_assert(index_of<T, 0, Args...>::value != -1, "the type is not exist");
    return std::get<index_of<T, 0, Args...>::value>(t);
}
int main()
{
    std::tuple<int, double, char, short> tp = std::make_tuple(1, 2.3, 'A', 8);
    auto r = get_element_by_type<short>(tp);
    std::cout<<r<<std::endl;
}
posted on 2017-01-05 19:29  abelian  阅读(231)  评论(0)    收藏  举报