博览网--STL与泛型编程(五)

一 偏特化实现hash function

型如:

template<>

struct hash


二 tuple 使用

tuple t;    XX: 为类型

tuple t (YY,YY,YY);  XX:为类型   YY:为具体值

make_tuple(XX,YY,ZZ); 

获取get(t)   XX为序号

课程中讲到的GNU C4.8 版本, tuple具体实现使用了递归的思路

最新版本gcc7.2 中的tuple由 

tuple 继承 _Tuple_impl  ,, _Tuple_impl  Basis case of inheritance recursion.

三 type traits

http://en.cppreference.com/w/cpp/header/type_traits

通过模板泛化,特化实现

四 cout

重载operator<<

五 movable

通过修改指针方式完成, 效率比较copy高,但是具有破化性。 


六 作业报错

重载operator<< , 输出参数使用版本参数, 报错如下


meter me

cout << me << endl;

^test.h:142:52: error: ‘std::ostream& weida1001::operator<<(std::ostream&)’ must take exactly two arguments std::ostream & operator << (ostream& os const T &m) { ^test.cpp: In function ‘void weida1001::test()’:test.cpp:21:8: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘weida1001::meter’)


//@{ /** * @brief Character inserters * @param __out An output stream. * @param __c A character. * @return out * * Behaves like one of the formatted arithmetic inserters described in * std::basic_ostream. After constructing a sentry object with good * status, this function inserts a single character and any required * padding (as determined by [22.2.2.2.2]). @c __out.width(0) is then * called. * * If @p __c is of type @c char and the character type of the stream is not * @c char, the character is widened before insertion. */ templateinline basic_ostream<_CharT, _Traits>&


参考 C++ include ostream 文件, 进行修改, 输出char 和string 不同, 我这里只是用char


template  < typename  _CharT, typename _Traits, typename T>

basic_ostream<_CharT,_Traits>& 

operator<<(basic_ostream<_CharT, _Traits>&os, const T &m)

 {

               unit_traitsu;

               os << m.getvalue() << u.get_unit(m);

}

你可能感兴趣的:(博览网--STL与泛型编程(五))