aboutsummaryrefslogtreecommitdiff
path: root/clang/test/SemaCXX/cxx2c-variadic-friends.cpp
blob: a4d7c8078338d2a6e22bef2468743f471003197e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2c %s

struct A;
struct B;
struct C;

struct S {};
template <typename> struct TS {};

template <typename ...Pack>
class X {
  friend Pack...;
  static void f() { } // expected-note {{declared private here}}
};

class Y {
  friend A, B, C;
  static void g() { } // expected-note {{declared private here}}
};

struct A {
  A() {
    X<A>::f();
    Y::g();
  };
};

struct B {
  B() {
    X<B, C>::f();
    Y::g();
  };
};

struct C {
  C() {
    X<A, B, C>::f();
    Y::g();
  };
};

struct D {
  D() {
    X<A, B, C>::f(); // expected-error {{'f' is a private member of 'X<A, B, C>'}}
    Y::g(); // expected-error {{'g' is a private member of 'Y'}}
  };
};

void f1() {
  A a;
  B b;
  C c;
  D d;
}

template <typename ...Pack>
struct Z {
  template <template <typename> class Template>
  struct Inner {
    friend Template<Pack>...;
  };
};

void f2() {
  Z<int, long, char> z;
  Z<int, long, char>::Inner<TS> inner;
}

namespace p2893r3_examples {
template<class... Ts>
class Passkey {
  friend Ts...;
  Passkey() {} // expected-note {{declared private here}}
};

class Foo;
class Bar;
class Baz;

class C {
public:
  void f(Passkey<Foo, Bar, Baz>);
};

class Foo {
  Foo() { C c; c.f({}); }
};

class Bar {
  Bar() { C c; c.f({}); }
};

class Baz {
  Baz() { C c; c.f({}); }
};

class Quux {
  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>'}}
};

template<class Derived, class MsgT>
struct Receiver {
  void receive(MsgT) {
    static_cast<Derived*>(this)->private_ += 1;
  }
};

template<class... MsgTs>
struct Dispatcher : Receiver<Dispatcher<MsgTs...>, MsgTs>... {
  using Receiver<Dispatcher, MsgTs>::receive...;
  friend Receiver<Dispatcher, MsgTs>...;

private:
  int private_;
};

void f() {
  Dispatcher<int, float> d;
  d.receive(0);
  d.receive(0.0f);
}
} // namespace p2893r3_examples

namespace p2893r3_note {
template <class... Ts> class R {
  friend Ts...;
};

template <class... Ts, class... Us>
class R<R<Ts...>, R<Us...>> {
  friend Ts::Nested..., Us...;
};

struct E { struct Nested; };
R<R<E>, R<C, int>> rr;
} // namespace p2893r3_note

namespace template_template {
template <typename U, template <typename> typename... Friend>
class S {
  friend class Friend<U>...;
  static constexpr int a = 42;
};

template <typename U>
struct T {
  static_assert(S<U, T>::a == 42);
  static_assert(S<U, T>::a == 43); // expected-error {{static assertion failed due to requirement 'S<int, template_template::T>::a == 43'}} \
                                   // expected-note {{expression evaluates to '42 == 43'}}
};

void f() {
  T<int> t; // expected-note {{in instantiation of}}
}
}