Skip to content

Wrong specialization becomes friend as checked by requires-expression #53364

Closed
@Fedr

Description

@Fedr

In the following code, there is template struct A with not-type template argument, and class B declares A<0> as its friend. Then the member function A<x>::has_access() is used to check that it has access to private B::p():

template<auto>
struct A {
    static constexpr bool has_access();
};

class B {
    void p() {}
    friend struct A<0>;
};

template<auto x>
constexpr bool A<x>::has_access() {
    return requires(B b) { b.p(); };
}

static_assert( !A<1>::has_access() );

And in Clang A<1> gets access to private fields of B. Not so in other compilers. Demo: https://gcc.godbolt.org/z/f7qPo6fnj

Related discussion: https://stackoverflow.com/q/70812488/7325599

Metadata

Metadata

Assignees

Labels

c++20clang:frontendLanguage frontend issues, e.g. anything involving "Sema"conceptsC++20 concepts

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions