| 1 |
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2c %s |
| 2 |
|
| 3 |
struct A; |
| 4 |
struct B; |
| 5 |
struct C; |
| 6 |
|
| 7 |
struct S {}; |
| 8 |
template <typename> struct TS {}; |
| 9 |
|
| 10 |
template <typename ...Pack> |
| 11 |
class X { |
| 12 |
friend Pack...; |
| 13 |
static void f() { } // expected-note {{declared private here}} |
| 14 |
}; |
| 15 |
|
| 16 |
class Y { |
| 17 |
friend A, B, C; |
| 18 |
static void g() { } // expected-note {{declared private here}} |
| 19 |
}; |
| 20 |
|
| 21 |
struct A { |
| 22 |
A() { |
| 23 |
X<A>::f(); |
| 24 |
Y::g(); |
| 25 |
}; |
| 26 |
}; |
| 27 |
|
| 28 |
struct B { |
| 29 |
B() { |
| 30 |
X<B, C>::f(); |
| 31 |
Y::g(); |
| 32 |
}; |
| 33 |
}; |
| 34 |
|
| 35 |
struct C { |
| 36 |
C() { |
| 37 |
X<A, B, C>::f(); |
| 38 |
Y::g(); |
| 39 |
}; |
| 40 |
}; |
| 41 |
|
| 42 |
struct D { |
| 43 |
D() { |
| 44 |
X<A, B, C>::f(); // expected-error {{'f' is a private member of 'X<A, B, C>'}} |
| 45 |
Y::g(); // expected-error {{'g' is a private member of 'Y'}} |
| 46 |
}; |
| 47 |
}; |
| 48 |
|
| 49 |
void f1() { |
| 50 |
A a; |
| 51 |
B b; |
| 52 |
C c; |
| 53 |
D d; |
| 54 |
} |
| 55 |
|
| 56 |
template <typename ...Pack> |
| 57 |
struct Z { |
| 58 |
template <template <typename> class Template> |
| 59 |
struct Inner { |
| 60 |
friend Template<Pack>...; |
| 61 |
}; |
| 62 |
}; |
| 63 |
|
| 64 |
void f2() { |
| 65 |
Z<int, long, char> z; |
| 66 |
Z<int, long, char>::Inner<TS> inner; |
| 67 |
} |
| 68 |
|
| 69 |
namespace p2893r3_examples { |
| 70 |
template<class... Ts> |
| 71 |
class Passkey { |
| 72 |
friend Ts...; |
| 73 |
Passkey() {} // expected-note {{declared private here}} |
| 74 |
}; |
| 75 |
|
| 76 |
class Foo; |
| 77 |
class Bar; |
| 78 |
class Baz; |
| 79 |
|
| 80 |
class C { |
| 81 |
public: |
| 82 |
void f(Passkey<Foo, Bar, Baz>); |
| 83 |
}; |
| 84 |
|
| 85 |
class Foo { |
| 86 |
Foo() { C c; c.f({}); } |
| 87 |
}; |
| 88 |
|
| 89 |
class Bar { |
| 90 |
Bar() { C c; c.f({}); } |
| 91 |
}; |
| 92 |
|
| 93 |
class Baz { |
| 94 |
Baz() { C c; c.f({}); } |
| 95 |
}; |
| 96 |
|
| 97 |
class Quux { |
| 98 |
Quux() { C c; c.f({}); } // expected-error {{calling a private constructor of class 'p2893r3_examples::Passkey<p2893r3_examples::Foo, p2893r3_examples::Bar, p2893r3_examples::Baz>'}} |
| 99 |
}; |
| 100 |
|
| 101 |
template<class Derived, class MsgT> |
| 102 |
struct Receiver { |
| 103 |
void receive(MsgT) { |
| 104 |
static_cast<Derived*>(this)->private_ += 1; |
| 105 |
} |
| 106 |
}; |
| 107 |
|
| 108 |
template<class... MsgTs> |
| 109 |
struct Dispatcher : Receiver<Dispatcher<MsgTs...>, MsgTs>... { |
| 110 |
using Receiver<Dispatcher, MsgTs>::receive...; |
| 111 |
friend Receiver<Dispatcher, MsgTs>...; |
| 112 |
|
| 113 |
private: |
| 114 |
int private_; |
| 115 |
}; |
| 116 |
|
| 117 |
void f() { |
| 118 |
Dispatcher<int, float> d; |
| 119 |
d.receive(0); |
| 120 |
d.receive(0.0f); |
| 121 |
} |
| 122 |
} // namespace p2893r3_examples |
| 123 |
|
| 124 |
namespace p2893r3_note { |
| 125 |
template <class... Ts> class R { |
| 126 |
friend Ts...; |
| 127 |
}; |
| 128 |
|
| 129 |
template <class... Ts, class... Us> |
| 130 |
class R<R<Ts...>, R<Us...>> { |
| 131 |
friend Ts::Nested..., Us...; |
| 132 |
}; |
| 133 |
|
| 134 |
struct E { struct Nested; }; |
| 135 |
R<R<E>, R<C, int>> rr; |
| 136 |
} // namespace p2893r3_note |
| 137 |
|
| 138 |
namespace template_template { |
| 139 |
template <typename U, template <typename> typename... Friend> |
| 140 |
class S { |
| 141 |
friend class Friend<U>...; |
| 142 |
static constexpr int a = 42; |
| 143 |
}; |
| 144 |
|
| 145 |
template <typename U> |
| 146 |
struct T { |
| 147 |
static_assert(S<U, T>::a == 42); |
| 148 |
static_assert(S<U, T>::a == 43); // expected-error {{static assertion failed due to requirement 'template_template::S<int, template_template::T>::a == 43'}} \ |
| 149 |
// expected-note {{expression evaluates to '42 == 43'}} |
| 150 |
}; |
| 151 |
|
| 152 |
void f() { |
| 153 |
T<int> t; // expected-note {{in instantiation of}} |
| 154 |
} |
| 155 |
} |
| 156 |
|