What functions are found
suggest changeFunctions are found by first collecting a set of “associated classes” and “associated namespaces” that include one or more of the following, depending on the argument type T. First, let us show the rules for classes, enumeration and class template specialization names.
- If
Tis a nested class or a member enumeration, the surrounding class is associated. - If
Tis an enumeration (it may also be a class member!), the innermost namespace of it. - If
Tis a class (it may also be nested!), all its base classes and the class itself. The innermost namespace of all associated classes. - If
Tis aClassTemplate<TemplateArguments>(this is also a class!), the classes and namespaces associated with the template type arguments, the namespace of any template template argument and the surrounding class of any template template argument, if a template argument is a member template.
Now there are a few rules for builtin types as well
- If
Tis a pointer toUor array ofU, the classes and namespaces associated withU. Example:void (*fptr)(A); f(fptr);, includes the namespaces and classes associated withvoid(A)(see next rule). - If
Tis a function type, the classes and namespaces associated with parameter and return types. Example:void(A)would include the namespaces and classes associated withA. - If
Tis a pointer to member, the classes and namespaces associated with the member type (may apply to both pointer to member functions and pointer to data member!). Example:B A::*p; void (A::*pf)(B); f(p); f(pf);includes the namespaces and classes associated withA,B,void(B)(which applies bullet above for function types).
Functions and function templates in the associated namespaces are found by argument dependent lookup. In addition, namespace-scope friend functions declared in associated classes are found, which are normally not visible. Using directives are ignored, however.
The calls of f below are valid without qualifying f. The call of g is not.
namespace A {
struct Z { };
namespace I { void g(Z); }
using namespace I;
struct X { struct Y { }; friend void f(Y) { } };
void f(X p) { }
void f(std::shared_ptr<X> p) { }
}
// example calls
f(A::X());
f(A::X::Y());
f(std::make_shared<A::X>());
g(A::Z()); // invalid: "using namespace I;" is ignored!
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents