Title
“matching specialization” for a friend declaration
Status
c++11
Section
13.7.5 [temp.friend]
Submitter
James Widman

Created on 2008-02-07.00:00:00 last changed 123 months ago

Messages

Date: 2010-08-23.00:00:00

[Voted into WP at August, 2010 meeting.]

Date: 2010-06-15.00:00:00

Proposed resolution (June, 2010):

Change 13.7.5 [temp.friend] bullet 1.3 as follows:

...For a friend function declaration that is not a template declaration:

  • ...

  • if the name of the friend is a qualified-id and a matching specialization of a function template is found in the specified class or namespace, the friend declaration refers to that function template specialization the deduced specialization of that function template (14.8.2.6 [temp.deduct.decl]), otherwise,

  • ...

(This resolution depends on that of issue 873; in particular, the cross-reference to 14.8.2.6 [temp.deduct.decl] refers to a new section added by the resolution of that issue.)

Date: 2022-02-18.07:47:23

13.7.5 [temp.friend] bullet 1.3 says:

  • if the name of the friend is a qualified-id and a matching specialization of a function template is found in the specified class or namespace, the friend declaration refers to that function template specialization, otherwise,

I'm not sure this says what it's supposed to say. For example:

    namespace N {
        template<class T> int f(T);
    }

    class A {
        friend int N::f(int);
        int m;
        A();
    };

    namespace N {
        template< class T > int f(T) {
            A a;            // ok for T=int?
            return a.m;     // ok for T=int?
        }
    }

    int m = N::f(42);       // ok?
    char c = N::f('a');     // Clearly ill-formed.

The key is that the wording talks about a “matching specialization,” which to me means that N::f<int> is befriended only if that specialization existed in N before the friend declaration. So it's ill-formed as written, but if we move the call to N::f<int> up to a point before the definition of A, it's well-formed.

That seems surprising, especially given that the first bullet does not require a pre-existing specialization. So I suggest replacing bullet 3 with something like:

  • if the name of the friend is a qualified-id and a matching function template is found in the specified class or namespace, the friend declaration refers to the deduced specialization of that function template, otherwise,

History
Date User Action Args
2014-03-03 00:00:00adminsetstatus: fdis -> c++11
2011-04-10 00:00:00adminsetstatus: wp -> fdis
2010-11-29 00:00:00adminsetstatus: dr -> wp
2010-08-23 00:00:00adminsetstatus: review -> dr
2010-03-29 00:00:00adminsetmessages: + msg2575
2010-02-16 00:00:00adminsetmessages: + msg2556
2010-02-16 00:00:00adminsetstatus: drafting -> review
2008-06-29 00:00:00adminsetstatus: open -> drafting
2008-02-07 00:00:00admincreate