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 !!!