设为首页 加入收藏

TOP

C++中函数类型的隐式转换在编写代码时发生错误问题解决
2017-12-07 14:22:00 】 浏览:255
Tags:函数 类型 转换 编写 代码 发生 错误 问题 解决

在编写如下代码时发生错误:

template
  
   
bool compare(T first, T second,function
   
    cp) { return cp(first,second); } string a("Wu"),b("Han"); cout<
    
     
> 编译的报错点在compare上,经过反复测试,发现问题出现在由lambda表达式实参到function类型的转换上。这种转换实际上是一种隐式转换。也就是说,在执行function的这种隐式转换前,函数并不能通过这种不同类型(一个是function,一个是lambda)的实参推断出形参的模板参数。

> 也就是说,只要我们能够在隐式转换前得到函数的形参模板参数,这种调用便可以正常发生。于是我做出如下几种修改:
//第一种方式
template
      
       
bool compare(T first,T second,function
       
        cp){ return cp(first,second); } string a("Wu"),b("Han"); cout<
        
         (a,b,[](string a,string b){ return a.size() < b.size(); });//调用函数时指定函数模板类型 ---------- //第二种方式 template
         
           bool compare(T first,T second,function
          
           cp){ return cp(first,second); } string a("Wu"),b("Han"); function
           
            > c = [](string a,string b){ return a.size() < b.size(); } cout<
            
              struct type_deduce{ using type T; }; template
             
               bool compare(T first,T second,typename type_deduce
              
               >::type cp){ return cp(first,second); } string a("Wu"),b("Han"); cout<
               
                
> 第三种情况有点特殊,我们没有显式的指定形参模板类型,我们靠着隐式转换实现了之前未能实现的操作。因为我们的function的模板依赖于前两个形参,也就是说,一旦我们知道了前面两个类型,我们便有了能够推断出的能力。但是我们必须让这个推断过程先于匹配过程,因此,我们加入了这个type_deduce。编译器解析完前两个参数,要确定第三个参数的在模板函数声明时候的类型(即type),被迫去算第三个参数的type_deduce中的type是什么,这样就把function的模板参数给解析出来了。


    到此,我的问题算是解决了,同样,我测试了隐式转换的几种情况,分别与以上的代码对应。
//测试一:隐式转换在模板间发生
template
                 
                  
class A{};

template
                  
                    class B { public: operator A
                   
                    (){return A
                    
                     ();} }; template
                     
                       void fun(A
                      
                        a){ std::cout<<"fun works"<
                       
                        ());//wrong B cannot be converted into A. A
                        
                          a = B
                         
                          (); fun(a);//ok return 0; } ---------- //测试二:当形参不为模板时,这种隐式转换发生 class A{}; template
                          
                            class B { public: operator A(){return A();} }; template
                           
                             void fun(A a){ std::cout<<"fun works"<
                            
                             ());//ok return 0; } ---------- //测试三:实现向模板形参的隐式转换,方法是借助已经解析的模板参数 template
                             
                               strcut type_deduce{ using type T; }; template
                              
                                class A{}; template
                               
                                 class B { public: operator A
                                
                                 (){return A
                                 
                                  ();} }; template
                                  
                                    void fun(T,typename type_deduce
                                   
                                    ()>::type){ std::cout<<"fun works"<
                                    
                                     ());//ok return 0; } 
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 


到现在,所有的测试及问题已经全部解决!!!

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇嵌入式系统组成的总结 下一篇C++ 删除数组中重复元素的算法(m..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目