c++模板元编程2(一)

2014-11-24 12:38:43 · 作者: · 浏览: 3

c++模板元编程第二章练习题

2-0. 编写一个一元元函数add_const_ref ,如果T是一个引用类型,就返回T,否则返回T
const&。编写一个程序来测试你的元函数。提示:可以使用boost::is_same来测试结果。

这个比较简单:

template
  
   
	struct add_const_ref
	{
		typedef T const& type;
	};

	template
   
     struct add_const_ref
    
      { typedef T& type; };
    
   
  
测试代码:

void fun_add_const_ref()
{
	typedef const int Cint;
	typedef const int& CRint;

	typedef int& Rint;
	if (boost::is_same
  
   ::type>::value)
	{
		std::cout << "true\n\n";
	}
	else
	{
		std::cout << "false\n\n";
	}

	if (boost::is_same
   
    ::type>::value) { std::cout << "true\n\n"; } { std::cout << "false\n\n"; } }
   
  



2-1. 编写一个三元元函数replace_type ,它接收一个任意的复合类型c作为其第一个参数,
并将c中出现的所有type x替换为y:
typedef replace_type< void*, void, int >::type t1; // int*
typedef replace_type<
int const*[10]
, int const
, long
>::type t2; // long* [10]
typedef replace_type<
char& (*)(char&)
, char&
, long&
>::type t3; // long& (*)(long&)
你可以将所操作的函数类型限制为具有少于两个参数的函数。

这个比较复杂:

分为4步解决

1. 首先判断c中是否含有类型x

2.如果有替换

3没有就返回元类型

4考虑一些模板特化

template
   
    
struct is_same: boost::mpl::bool_
    
      { };//#1 template
     
       struct is_same
      
        : boost::mpl::bool_
       
         { };//#1 template
        
          struct replace_type_imp;//#2 template
         
           struct replace_type { static bool const value = is_same
          
           ::value;//#3 typedef typename replace_type_imp
           
            ::type type;//#4 };
           
          
         
        
       
      
     
    
   
#1判断两个类型时候为同一个类型 如果是返回true,否则返回false

#2replace_type的具体实现,包含一个特化情况

#3返回时候相同value记录返回结果

#4根据vlaue的值返回类型

下面是一般类型的实现:

////特化void
//TC:void const*, TX:void const
template
   
    
struct replace_type_imp
    
      { typedef typename replace_type
     
      ::type type(); }; //特化TC* //TC:int const*, TX:int const template
      
        struct replace_type_imp
       
         { typedef typename replace_type
        
         ::type* type; }; //特化TC& //TC::int const&, TX:int const template
         
           struct replace_type_imp
          
            { typedef typename replace_type
           
            ::type& type; }; //特化TC[] //TC::int const[], TX:int const template
            
              struct replace_type_imp
             
               { typedef typename replace_type
              
               ::type type[]; }; //特化TC[N] //TC::int const[N], TX:int const template
               
                 struct replace_type_imp
                
                  { typedef typename replace_type
                 
                  ::type type[N]; };
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   


函数的特化

//接受一个参数
//TC:: char* (*)(char*), TX: char*, TY: int
template
   
    
struct replace_type_imp
    
      { typedef typename replace_type
     
      ::type type(typename replace_type
      
       ::type);//#1 //#1处怎么能定义成一个 函数指针 }; //接受两个参数 //TC:: char* (*)(char*, const char* ), TX: char*, TY:: int template
       
         struct replace_type_imp
        
          { typedef typename replace_type
         
          ::type type(typename replace_type
          
           ::type, typename replace_type
           
            ::type); }; //接受三个函数 template
            
              struct replace_type_imp
             
               { typedef typename replace_type
              
               ::type type(typename replace_type
               
                ::type, typename replace_type
                
                 ::type, typename replace_type
                 
                  ::type); }; //...接受任一多参数 
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   

value 为true:

template
   
    
struct replace_type_imp
    
      { typedef TY type; };
    
   

最后测试

void fun_is_same()
{
	typedef char& (*FunPoint[])(char&);
	typedef char& Rchar;
	typedef const int v1;
	typedef const int v2;
	typedef int* v3;
	typedef int* (*IntPoint[])(int*);

	if (is_same
   
    ::value) std::cout << "same\n\n";
	else std::cout << "false\n\n" << std::endl;

	if (boost::is_same
    
     ::value) std::cout << "same\n\n"; else std::cout << "false\n\n" << std::endl; typedef replace_type
     
      ::type v4; if (is_same
      
       ::value) std::cout << "same\n\n"; else std::cout << "false\n\n" << std::endl; if (boost::is_same
       
        ::value) std::cout << "same\n\n"; else std::cout << "false\n\n" << std::endl; typedef replace_type
        
         ::type v5; if (is_same
         
          ::value) std::cout << "same\n\n"; else std::cout << "false\n\n" << std::endl; if (boost::is_same
          
           ::value) std::cout << "same\n\n"; else std::cout << "false\n\n" << std::endl; typedef replace_type
           
            ::type v6; if (is_same
            
             ::value) std::cout << "same\n\n"; else std::cout << "false\n\n" << std::endl; if (boost::is_same
             
              ::value) std::cout << "same\n\n"; else std::cout << "false\n\n" << std::endl; } 
             
            
           
          
         
        
       
      
     
    
   

2-2. boost::polymorphic_downcast
函数模板
实现一个带检查版本的static_cast,用于将指向多态
对象的指针向下转型:
template inline Target polymorphic_downcast(Source* x)
{
assert( dynamic_cast (x) == x );
return static_cast (x);
}
在发行版的软件中,assertion消失并且