TITLE: function lookup in namespaces Issue #018 December, 1996 Contents: Notes From ANSI/ISO - Function Lookup in Namespaces Notes From ANSI/ISO - State of the C++ Standard Introduction to Exception Handling Part 4 - Handling an Exception Using C++ as a Better C Part 18 - Hiding Names Introduction to STL Part 5 - Bit Sets NOTES FROM ANSI/ISO - FUNCTION LOOKUP IN NAMESPACES Jonathan Schilling, jls@sco.com An important change has recently been made in the way functions are found within namespaces. The three basic ways of making the contents of a namespace visible were discussed in C++ Newsletters #002 and #004. These are: explicit qualification, using directives, and using declarations. Consider the following namespace, which declares a class and some (non-member) functions: namespace N { class A { ... }; A& operator+(const A&, const A&); void f(A); void g(); } Now consider the following function that takes arguments of the class type: void z(N::A x, N::A y) { x + y; // (1) f(x); // (2) g(); // (3) } Given the original rules for namespaces (just the three basic methods of namespace visibility), all three of the statements in this function are compilation errors, because none of the functions being called are visible. However the standards committee has changed the way functions are looked up. Now there is a new language rule, which says that the namespaces of the classes of the arguments, and the namespaces of the base classes of those classes, are included in the search for function declarations, even when the contents of those namespaces are not otherwise visible. So, when looking for an operator+() in (1) above, the arguments are x and y, the class of those arguments is A, and A is declared in namespace N. Thus the compiler looks for an operator+() in N, and finds one, and the call is legal. A similar process happens for the call to f() in (2). However, the call to g() in (3) is still a compilation error, because there are no arguments to direct the lookup. The call would have to be made using one of the basic methods: N::g(); // explicit qualification If the arguments to the function have different types, then all the associated namespaces are searched. Arguments of built-in types such as int have no associated namespace, while arguments of more complicated types such as pointers to functions bring in the namespaces of the pointed-to function's parameters and return type. This new lookup rule was first added to solve some technical language definition problems with operator functions. It was then added to solve some other problems with template "dependent name" lookup (see C++ Newsletter #017) and template friends. At that point it was felt that consistency demanded the new rule be extended to lookup of all functions in all contexts, and this was done (albeit with some dissent within the committee) at the Stockholm meeting in July. Because of the staggered introduction of this rule, for a while you may encounter compilers that implement it for operator functions but not for other functions, but eventually all implementations will be in full conformance. One important thing to note about this rule change is that it is a step toward making namespaces a powerful scoping and packaging construct, rather than just a transparent vehicle to avoid name collisions. The art of employing namespaces is still in its early stages, and first reports have indicated that the basic methods of making names visible are sometimes too verbose (explicit qualification), too broad (using directives), or too prone to error and omission (using declarations). The new rule may help alleviate some of these problems. NOTES FROM ANSI/ISO - STATE OF THE C++ STANDARD Jonathan Schilling, jls@sco.com [snip] INTRODUCTION TO EXCEPTION HANDING PART 4 - HANDLING AN EXCEPTION [snip] USING C++ AS A BETTER C PART 18 - HIDING NAMES [snip] INTRODUCTION TO STL PART 5 - BIT SETS [snip] ACKNOWLEDGEMENTS [snip] SUBSCRIPTION INFORMATION / BACK ISSUES To subscribe to the newsletter, send mail to majordomo@world.std.com with this line as its message body: subscribe c_plus_plus Back issues are available via FTP from: rmi.net /pub2/glenm/newslett or on the Web at: http://rainbow.rmi.net/~glenm There is also a Java newsletter. To subscribe to it, say: subscribe java_letter using the same majordomo@world.std.com address. ------------------------- Copyright (c) 1996 Glen McCluskey. All Rights Reserved. This newsletter may be further distributed provided that it is copied in its entirety, including the newsletter number at the top and the copyright and contact information at the bottom. Glen McCluskey & Associates Professional C++ Consulting Internet: glenm@glenmccl.com Phone: (800) 722-1613 or (970) 490-2462 Fax: (970) 490-2463 FTP: rmi.net /pub2/glenm/newslett (for back issues) Web: http://rainbow.rmi.net/~glenm