Skip to content

Incorrect and stateful access control for user-defined conversion in function template default argument initialization #62444

Closed
@ecatmur

Description

@ecatmur

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>();

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions