Item 25. Argument Dependent Lookup

xiaoxiao2021-03-05  24

Item 25. Argument Dependent Lookup

NameSpaces has a deep impact on C programs and designs. Its Argument Dependent Lookup (ADL) feature is very important, despite the potential increases the complexity of the program, the problem it solves is much more than it introduced.

ADL's idea is very simple: When you find the name of the function in the function call expression, the compiler will check the Namespaces in the function parameter type. EG:

Namespace org_semantics {class x {...}; void f (const x "; void g (x *); x operator (const x &, const x"; class string {...}; std :: Ostream Operator << (std :: ostream &, const string ";} // ... int G (org_semantics :: x *); void AFUNC () {org_semantics :: x a; f (a); // 1) Call org_semantics :: f, because the type of A is org_semantics :: x, so find G (& a); // 2) Error! Ambiguous ... a = a a; // 3) in // org_semantics Call org_semantics :: Operator }

According to the call according to the ADL, 1), 3), 2) Why is there a problem? Complicated rules like ADL will also make people taking blood flow. When the function g is called. In this case, the encoder will find the global G by the compiler, but because the parameters of the G is org_semantics :: x *, it will find a G under Namespaces org_Semantics, so Ambiguous happened. Solution: The name of one of them.

转载请注明原文地址:https://www.9cbs.com/read-35845.html

New Post(0)