模板的引入 使c++產(chǎn)生了泛型的算法 泛型的容器
這兩個(gè)是個(gè)好東西 但是在將兩項(xiàng)技術(shù)結(jié)合的時(shí)候產(chǎn)生了一個(gè)問(wèn)題 就是在寫(xiě)程序的時(shí)候要暴露對(duì)象的類(lèi)型
就像這個(gè)樣子 :
template<typename T>
vector<T>::iterator find(const T& )
{
........
}
而如果暴露了類(lèi)型的話(huà) 那么就不能寫(xiě)一段代碼而完成問(wèn)題了 必須為每種容器都寫(xiě)出相同的算法
怎么辦呢 可以在find中傳出兩個(gè)參數(shù)
template<typename T>
vector<T>::iterator find(const T&, T& result )
{
........
}
但是好像還是有問(wèn)題 譬如類(lèi)型的引用 還有size 指針 等等 都是不同的類(lèi)型 寫(xiě)起來(lái)還是很頭大
于是 聰明的人們想出了type traits這項(xiàng)技術(shù)
這項(xiàng)技術(shù)
typename<typename T>
class iterator
{
public:
typedef T value_type
}
typename<typename I>
typename I::value_type
find(I &)
{
}
與STL的實(shí)現(xiàn)不同 在boost中 他們使用的是模板片特化來(lái)實(shí)現(xiàn)的type traits
基本思想就是 默認(rèn)大部分都不支持某種特性 然后 當(dāng)某個(gè)類(lèi)型支持時(shí)就為他特化一個(gè)類(lèi) 支持這樣的特性
感覺(jué)這樣寫(xiě)的話(huà) 在特化的時(shí)候會(huì)不會(huì)代碼會(huì)比較多呢 ...
具體就是
template<typename T>
class something
{
// 在這里寫(xiě)對(duì)廣大的類(lèi)的操作
}
然后對(duì)于特殊的類(lèi)型 譬如說(shuō) int
template<>
class something<int>
{
//然后在這里寫(xiě)對(duì)Int做的特殊的操作
}
而為了type traits來(lái)說(shuō) 它的特殊操作就是在泛型類(lèi)里面做一個(gè)枚舉 值為false而在 int里面這個(gè)值為true
那么 當(dāng)算法來(lái)用這個(gè)對(duì)象時(shí) 可以根據(jù)這個(gè)枚舉的值來(lái)選擇相應(yīng)的算法 ,譬如對(duì)int的速度比較快的 或者對(duì)泛型的正確的算法
下面是 std::swap的一個(gè)優(yōu)化版本
//
// iter_swap:
// tests whether iterator is a proxying iterator or not, and
// uses optimal form accordingly:
//
namespace detail{
template <typename I>
static void do_swap(I one, I two, const boost::false_type&)
{
typedef typename std::iterator_traits<I>::value_type v_t;
v_t v = *one;
*one = *two;
*two = v;
}
template <typename I>
static void do_swap(I one, I two, const boost::true_type&)
{
using std::swap;
swap(*one, *two);
}
}
template <typename I1, typename I2>
inline void iter_swap(I1 one, I2 two)
{
//
// See is both arguments are non-proxying iterators,
// and if both iterator the same type:
//
typedef typename std::iterator_traits<I1>::reference r1_t;
typedef typename std::iterator_traits<I2>::reference r2_t;
typedef boost::integral_constant<bool,
::boost::is_reference<r1_t>::value
&& ::boost::is_reference<r2_t>::value
&& ::boost::is_same<r1_t, r2_t>::value> truth_type;
detail::do_swap(one, two, truth_type());
}
其中 boost::integral_constant<bool, ::boost::is_reference<r1_t>::value
&& ::boost::is_reference<r2_t>::value
&& ::boost::is_same<r1_t, r2_t>::value> truth_type
就是一個(gè)枚舉 看傳入的參數(shù)是否可以用std::swap 如果可以則連接到std::swap不然就用自己寫(xiě)的 呵呵
這兩個(gè)是個(gè)好東西 但是在將兩項(xiàng)技術(shù)結(jié)合的時(shí)候產(chǎn)生了一個(gè)問(wèn)題 就是在寫(xiě)程序的時(shí)候要暴露對(duì)象的類(lèi)型
就像這個(gè)樣子 :
template<typename T>
vector<T>::iterator find(const T& )
{
........
}
而如果暴露了類(lèi)型的話(huà) 那么就不能寫(xiě)一段代碼而完成問(wèn)題了 必須為每種容器都寫(xiě)出相同的算法
怎么辦呢 可以在find中傳出兩個(gè)參數(shù)
template<typename T>
vector<T>::iterator find(const T&, T& result )
{
........
}
但是好像還是有問(wèn)題 譬如類(lèi)型的引用 還有size 指針 等等 都是不同的類(lèi)型 寫(xiě)起來(lái)還是很頭大
于是 聰明的人們想出了type traits這項(xiàng)技術(shù)
這項(xiàng)技術(shù)
typename<typename T>
class iterator
{
public:
typedef T value_type
}
typename<typename I>
typename I::value_type
find(I &)
{
}
與STL的實(shí)現(xiàn)不同 在boost中 他們使用的是模板片特化來(lái)實(shí)現(xiàn)的type traits
基本思想就是 默認(rèn)大部分都不支持某種特性 然后 當(dāng)某個(gè)類(lèi)型支持時(shí)就為他特化一個(gè)類(lèi) 支持這樣的特性
感覺(jué)這樣寫(xiě)的話(huà) 在特化的時(shí)候會(huì)不會(huì)代碼會(huì)比較多呢 ...
具體就是
template<typename T>
class something
{
// 在這里寫(xiě)對(duì)廣大的類(lèi)的操作
}
然后對(duì)于特殊的類(lèi)型 譬如說(shuō) int
template<>
class something<int>
{
//然后在這里寫(xiě)對(duì)Int做的特殊的操作
}
而為了type traits來(lái)說(shuō) 它的特殊操作就是在泛型類(lèi)里面做一個(gè)枚舉 值為false而在 int里面這個(gè)值為true
那么 當(dāng)算法來(lái)用這個(gè)對(duì)象時(shí) 可以根據(jù)這個(gè)枚舉的值來(lái)選擇相應(yīng)的算法 ,譬如對(duì)int的速度比較快的 或者對(duì)泛型的正確的算法
下面是 std::swap的一個(gè)優(yōu)化版本
//
// iter_swap:
// tests whether iterator is a proxying iterator or not, and
// uses optimal form accordingly:
//
namespace detail{
template <typename I>
static void do_swap(I one, I two, const boost::false_type&)
{
typedef typename std::iterator_traits<I>::value_type v_t;
v_t v = *one;
*one = *two;
*two = v;
}
template <typename I>
static void do_swap(I one, I two, const boost::true_type&)
{
using std::swap;
swap(*one, *two);
}
}
template <typename I1, typename I2>
inline void iter_swap(I1 one, I2 two)
{
//
// See is both arguments are non-proxying iterators,
// and if both iterator the same type:
//
typedef typename std::iterator_traits<I1>::reference r1_t;
typedef typename std::iterator_traits<I2>::reference r2_t;
typedef boost::integral_constant<bool,
::boost::is_reference<r1_t>::value
&& ::boost::is_reference<r2_t>::value
&& ::boost::is_same<r1_t, r2_t>::value> truth_type;
detail::do_swap(one, two, truth_type());
}
其中 boost::integral_constant<bool, ::boost::is_reference<r1_t>::value
&& ::boost::is_reference<r2_t>::value
&& ::boost::is_same<r1_t, r2_t>::value> truth_type
就是一個(gè)枚舉 看傳入的參數(shù)是否可以用std::swap 如果可以則連接到std::swap不然就用自己寫(xiě)的 呵呵