In C ++, implicit conversion of function types occurs when code is written. The problem is solved, and the function has an error.

Source: Internet
Author: User

In C ++, implicit conversion of function types occurs when code is written. The problem is solved, and the function has an error.

An error occurred when writing the following code:

Template
 
  
Bool compare (T first, T second, function
  
   
Cp) {return cp (first, second);} string a ("Wu"), B ("Han"); cout <
   
    
> The error message for compiling is on compare. After repeated tests, the problem occurs when the lambda expression converts the real parameters to the function type. This conversion is actually an implicit conversion. That is to say, before performing this implicit conversion of a function, the function cannot deduce the template parameters of the form parameter through the real parameters of different types (one is function and the other is lambda.
> That is, as long as we can obtain the parameter template of the function before implicit conversion, this call can occur normally. So I made the following changes:
// Method 1 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. size () <B. size () ;}); // specify the function template type when calling the function ---------- // template method 2
        
         
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 <
              
               
> The third case is a bit special. We didn't explicitly specify the parameter template type. We implemented the previously unimplemented operations by implicit conversion. Because our function template depends on the first two parameters, that is, once we know the first two types, we have the ability to deduce them. However, we must make this inference process prior to the matching process. Therefore, we have added this type_deduce. After parsing the first two parameters, the compiler determines the type of the third parameter in the template function declaration, and is forced to calculate the type in the type_deduce of the third parameter, in this way, the function template parameters are parsed.
At this point, my problem is solved. Similarly, I tested several situations of implicit conversion, which correspond to the above Code respectively.
// Test 1: template occurs between templates for implicit conversion.
                
                 
Class A {}; template
                 
                  
Class B {public: operator
                  
                   
() {Return
                   
                    
() ;}}; Template
                    
                     
Void fun (
                     
                       A) {std: cout <"fun works" <
                      
                        (); // Wrong B cannot be converted into A.
                       
                         A = B
                        
                          (); Fun (a); // OK return 0;} ---------- // Test 2: class A {}; template is used for this implicit conversion when the form parameter is not a template.
                         
                           Class B {public: operator A () {return A () ;}}; template
                          
                            Void fun (A a) {std: cout <"fun works" <
                           
                             (); // OK return 0;} ---------- // Test 3: Implement implicit conversion to the template parameters by using the parsed template parameter template
                            
                              Strcut type_deduce {using type T;}; template
                             
                               Class A {}; template
                              
                                Class B {public: operator
                               
                                 () {Return
                                
                                  () ;}}; Template
                                 
                                   Void fun (T, typename type_deduce
                                  
                                    () >:: Type) {std: cout <"fun works" <
                                   
                                     (); // OK return 0 ;}
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                

By now, all tests and problems have been solved !!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.