健康,快乐,勇敢的宁帅!!

努力、努力、再努力! 没有什么能阻止我对知识的渴望。

 

"C++Templates The Complete Guide"读书笔记----Chapter 2

这段时间在通过"C++Templates The Complete Guide"这本书学习Templates。发现这本书确实不错:语言简明,内容翔实。特别是每章后面的Summery总结得很好。这份读书笔记就已这个Summery为基础写的。

书前面的Prefece和Chapter 1就跳过了。既然来学习Templates就已经知道它的重要性了。

                                Chapter 2 FunctionTemplates
1. Function templates define a family of functions for different template arguments;

template  < typename T >
inline T 
const &  max (T  const &  a, T  const &  b)
{
    
//  if a < b then use b else use a
     return   a  <  b  ?  b : a;
}

2. When you pass template arguments, function templates are instantiated for these argument types.
The process of replacing templates parameters by concrete types is called instantiatin.(at compiled time)
3. You can explicitly qualify the template parameters

{
//  because no automatic type conversion if allowed in templates,so
max(static_cast < double > ( 4 ), 4.2 ) // cast the arguments so that they both match
max < double > ( 4 , 4.2 ) //  specify explicitly the type of T
}

4. You can overload funciton templates

inline  int   const &  max ( int   const &  a,  int   const &  b) 
{
    
return   a  <  b  ?  b : a;
}


//  maximum of two values of any type
template  < typename T >
inline T 
const &  max (T  const &  a, T  const &  b)
{
    
return   a  <  b  ?  b : a;
}


//  maximum of three values of any type
template  < typename T >
inline T 
const &  max (T  const &  a, T  const &  b, T  const &  c)
{
    
return  ::max (::max(a,b), c);
}


int  main()
{
    ::max(
7 42 68 );      //  calls the template for three arguments
    ::max( 7.0 42.0 );      //  calls max<double> (by argument deduction)
    ::max( ' a ' ' b ' );       //  calls max<char> (by argument deduction)
    ::max( 7 42 );          //  calls the nontemplate for two ints
    ::max <> ( 7 42 );        //  calls max<int> (by argument deduction)
    ::max < double > ( 7 42 );  //  calls max<double> (no argument deduction)
    ::max( ' a ' 42.7 );      //  calls the nontemplate for two ints
}

ps: the overload resolution process normally prefers this nontemplate over one generated from the template. the fourth call falls under this rule.
5. When you overload function templates, limit your changes to specifying template parameters explicitly

{
max
<> ( 7 , 42 ); // call max<int> (by argument deduction)
}


6. Make sure you see all overloaded versions of funciton templates before you call them

template  < typename T >
inline T 
const &  max (T  const &  a, T  const &  b)
{
    
return   a  <  b  ?  b : a;
}


//  maximum of three values of any type
template  < typename T >
inline T 
const &  max (T  const &  a, T  const &  b, T  const &  c)
{
    
return  max (max(a,b), c);   //  uses the template version even for ints
}
                               //  because the following declaration comes
                               
//  too late:
//  maximum of two int values
inline  int   const &  max ( int   const &  a,  int   const &  b) 
{
    
return   a  <  b  ?  b : a;
}


 

posted on 2006-11-26 15:24 ningfangli 阅读(169) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

统计

公告

Dict.CN 在线词典, 英语学习, 在线翻译

常用链接

留言簿(4)

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜