When C ++ is overloaded, non-module functions are preferentially matched.

Source: Internet
Author: User

It is found that when there are enough overload functions,

When a non-module function or a module function can be matched,

GCC seems to give priority to non-module functions.

See the followingCode

# Include <iostream> # Include <Vector> # Include < String > # Include <Limits> Using   Namespace  STD;  Int Max ( Const   Int N1, Const  Int  N2 ){  Return N1> N2? N1: N2 ;}  Float Max ( Const   Float F1, Const   Float  F2 ){  Return F1> F2? F1: F2 ;}  String Max ( Const   String Str1,Const    String  Str2 ){  Return Str1> str2? Str1: str2 ;}  # If 0 Int Max ( Const Vector < Int > & VEC ){  Return * Max_element (VEC. Begin (), VEC. End ());}  Float Max ( Const Vector < Float > & VEC ){  Return * Max_element (VEC. Begin (), VEC. End ());}  String Max ( Const Vector < String > & VEC ){  Return * Max_element (VEC. Begin (), VEC. End ());}  Int Max ( Const   Int * Array,Int  Size ){  Int Int_max = numeric_limits < Int > : Min ();  For ( Int I = 0 ; I <size; ++ I) {int_max = Max (int_max, array [I]);}  Return  Int_max ;}  Float Max ( Const  Float * Array, Int  Size ){  Float Float_max = numeric_limits < Float > : Min ();  For ( Int I = 0 ; I <size; ++ I) {float_max = Max (float_max, array [I]);}  Return  Float_max ;} String Max ( Const   String * Array, Int  Size ){  Return * Max_element (array, array + Size );}  # Endif  Template <Typename T> T max (  Const Vector <t> & VEC) {cout < "  Invoke " < Endl;  Return * Max_element (VEC. Begin (), VEC. End ();} Template <Typename T> T max (  Const T * array, Int  Size ){  Return * Max_element (array, array + Size );}  Int Main ( Int Argc, Const  Char * Argv []) {cout < "  Test overload Max  " < Endl;  Int N1 = 3  ;  Int N2 = 9  ;  Float F1 = 3.14  ; Float F2 = 98.44  ;  String Str1 = "  S1  "  ;  String Str2 = "  ABC  "  ; Cout < "  Max int (  " <N1 < "   " <N2 < "  )  " <Max (N1, N2) < Endl < "  Max float (  " <F1 < "   " <F2 < "  )  " <Max (F1, F2) < Endl < "  Max string (  " <Str1 < "   " <Str2 < "  )  " < Max (str1, str2) < Endl;  Int Int_array [] = {33 , 444 , 5 , 322 , 124 , 55 , 3436  }; Vector < Int > Int_vec (int_array, int_array + Sizeof (Int_array )/ Sizeof ( Int  ); Cout <"  The Max number of the int vector is:  " <Max (int_vec) < Endl;  Float F_array [] = { 34.24 , 4324.43 , 9999.43 , 556.34  }; Vector < Float > F_vec (f_array, f_array + Sizeof (F_array )/ Sizeof ( Float  ); Cout < "  The Max number of the float vector is:  " <Max (f_vec) < Endl;  String Str_array [] = { "  Boog  " , "  Gogod  " , " Zero  " , "  Loe  " , "  Abcdasdfas  "  ,  " Please refer to the following link for more information:"  }; Vector < String > Str_vec (str_array, str_array + Sizeof (Str_array )/ Sizeof ( String  ); Cout < "  The max number of the string vector is:  " <Max (str_vec) < Endl;  # If 1 Cout <"  The Max number of the int array is:  " <Max (int_array, Sizeof (Int_array )/ Sizeof ( Int ) < Endl; cout < "  The max number of the flaot array is:  " <Max (f_array, Sizeof (F_array )/ Sizeof (Float ) < Endl; cout < "  The Max number of the string array is:  " <Max (str_array, Sizeof (Str_array )/ Sizeof (Str_array [ 0  ]) < "  Sizes =  " < Sizeof (Str_array )/Sizeof (Str_array [ 0  ]) < "  Sizes of str_array  " < Sizeof  (Str_array) < "  Sizes of str_array [0]  " < Sizeof (Str_array [ 0  ]) < Endl;  # Endif      Return   0  ;} 

Output

Test overload Max
Max int (3 9) 9
Max float (3.14 98.44) 98.44
Max string (S1 ABC) S1
Invoke
The Max number of the int vector is: 3436
Invoke
The Max number of the float vector is: 9999.43
Invoke
The max number of the string vector is: Zero
The Max number of the int array is: 3436
The max number of the flaot array is: 9999.43
The Max number of the string array is: Zero sizes = 6 sizes of str_array 24 sizes of str_array [0] 4

 

 

If # If 0 is enabled, no non-module function is called by invoke.

Related Article

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.