Closed
Description
Clang accepts-invalid:
struct B { friend struct A; private: B(int); };
template<class T> int f(T = 0);
struct A { A() { int i = f<B>(); } };
Per http://eel.is/c++draft/temp.inst#12.sentence-1 access control should be the same as for a function template specialization adjacent to f
; the friendship of A
is irrelevant. MSVC and gcc get this right, although gcc weirdly accepts-invalid if the ctor is B()
and the default argument initializer is {}
.
Worse, clang correctly rejects if the caller is not a friend:
struct B { friend struct A; private: B(int); };
template<class T> int f(T = 0);
int i = f<B>(); // error: calling a private constructor of class 'B'
but if the friend call is moved first it accepts, indicating that the instantiated default argument has been remembered, which would be fine if it didn't use A
's access control.
struct B { friend struct A; private: B(int); };
template<class T> int f(T = 0);
struct A { A() { int i = f<B>(); } };
int i = f<B>();