这段时间在通过"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
				>
				template 
				<
				typename T
				>
				
						
						 inline T 
				const
				&
				 max (T 
				const
				&
				 a, T 
				const
				&
				 b)
inline T 
				const
				&
				 max (T 
				const
				&
				 a, T 
				const
				&
				 b)

 
				
						 {
				
				
						{
 //
						 if a < b then use b else use a
    
						//
						 if a < b then use b else use a
						
								
								 return
						  a 
						<
						 b 
						?
						 b : 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
						//
						 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(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
						
						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)
				inline 
				int
				 
				const
				&
				 max (
				int
				 
				const
				&
				 a, 
				int
				 
				const
				&
				 b) 

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

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

 
				
						 {
				
				
						{
 return
						 ::max (::max(a,b), c);
    
						return
						 ::max (::max(a,b), c);
 }
}
				
				
						
						 
						
						 int
				 main()
				
				int
				 main()

 
				
						 {
				
				
						{
 ::max(
						7
						, 
						42
						, 
						68
						);     
						//
						 calls the template for three arguments
    ::max(
						7
						, 
						42
						, 
						68
						);     
						//
						 calls the template for three arguments
						
								
								 ::max(
						7.0
						, 
						42.0
						);     
						//
						 calls max<double> (by argument deduction)
						
						    ::max(
						7.0
						, 
						42.0
						);     
						//
						 calls max<double> (by argument deduction)
						
								
								 ::max(
						'
						a
						'
						, 
						'
						b
						'
						);      
						//
						 calls max<char> (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 the nontemplate for two ints
						
								
								 ::max
						<>
						(
						7
						, 
						42
						);       
						//
						 calls max<int> (by argument deduction)
						
						    ::max
						<>
						(
						7
						, 
						42
						);       
						//
						 calls max<int> (by argument deduction)
						
								
								 ::max
						<
						double
						>
						(
						7
						, 
						42
						); 
						//
						 calls max<double> (no argument deduction)
						
						    ::max
						<
						double
						>
						(
						7
						, 
						42
						); 
						//
						 calls max<double> (no argument deduction)
						
								
								 ::max(
						'
						a
						'
						, 
						42.7
						);     
						//
						 calls the nontemplate for two ints
						
						    ::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)
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
				>
				template 
				<
				typename T
				>
				
						
						 inline T 
				const
				&
				 max (T 
				const
				&
				 a, T 
				const
				&
				 b)
inline T 
				const
				&
				 max (T 
				const
				&
				 a, T 
				const
				&
				 b)

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

 
				
						 {
				
				
						{
 return
						  a 
						<
						 b 
						?
						 b : a;
    
						return
						  a 
						<
						 b 
						?
						 b : a;
 }
}