
Jean-Francois Bastien wrote:
[pedanticism] Don't both your examples rely on pre-standard C++ friend declarations and definitions injecting the function name in the innermost enclosing namespace? I might be reading this worng, but 7.3.1.2 [namespace.memdef] paragraph 3 states: "The name of the friend is not found by unqualified lookup (3.4.1) or by qualified lookup (3.4.3) until a matching declaration is provided in that namespace scope". So you'd still need to declare in the namespace. From what I understand the examples shouldn't work.
Correct me if I'm wrong :)
Well... I do appreciate your pedanticism, but I think you're wrong. All the compilers I tried accept the function calls to the inline friend function, operator-(safe_int,safe_int), including MSVC 2008 SP1, Comeau (www.comeaucomputing.com/tryitout), and g++ 4.1.2, run at http://codepad.org/dB9IbQJ7 You might want to have a look at [temp.inject] ("Friend names declared within a class template"). Quoting the latest C++0x Working Draft, www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2857.pdf, 14.7.5/2 (slightly reformatted): "As with non-template classes, the names of namespace-scope friend functions of a class template specialization are not visible during an ordinary lookup unless explicitly declared at namespace scope (11.4). Such names may be found under the rules for associated classes (3.4.2). [ Example: template<typename T> struct number { number(int); friend number gcd(number x, number y) { return 0; }; }; void g() { number<double> a(3), b(4); a = gcd(a,b); // finds gcd because number<double> is an // associated class, making gcd visible // in its namespace (global scope) b = gcd(3,4); // ill-formed; gcd is not visible } —end example ] Kind regards, Niels