xref: /aosp_15_r20/external/clang/test/SemaTemplate/instantiate-member-pointers.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
2*67e74705SXin Li struct Y {
3*67e74705SXin Li   int x;
4*67e74705SXin Li };
5*67e74705SXin Li 
6*67e74705SXin Li template<typename T>
7*67e74705SXin Li struct X1 {
fX18*67e74705SXin Li   int f(T* ptr, int T::*pm) { // expected-error{{member pointer}}
9*67e74705SXin Li     return ptr->*pm;
10*67e74705SXin Li   }
11*67e74705SXin Li };
12*67e74705SXin Li 
13*67e74705SXin Li template struct X1<Y>;
14*67e74705SXin Li template struct X1<int>; // expected-note{{instantiation}}
15*67e74705SXin Li 
16*67e74705SXin Li template<typename T, typename Class>
17*67e74705SXin Li struct X2 {
fX218*67e74705SXin Li   T f(Class &obj, T Class::*pm) { // expected-error{{to a reference}} \
19*67e74705SXin Li                       // expected-error{{member pointer to void}}
20*67e74705SXin Li     return obj.*pm;
21*67e74705SXin Li   }
22*67e74705SXin Li };
23*67e74705SXin Li 
24*67e74705SXin Li template struct X2<int, Y>;
25*67e74705SXin Li template struct X2<int&, Y>; // expected-note{{instantiation}}
26*67e74705SXin Li template struct X2<const void, Y>; // expected-note{{instantiation}}
27*67e74705SXin Li 
28*67e74705SXin Li template<typename T, typename Class, T Class::*Ptr>
29*67e74705SXin Li struct X3 {
operator =X330*67e74705SXin Li   X3<T, Class, Ptr> &operator=(const T& value) {
31*67e74705SXin Li     return *this;
32*67e74705SXin Li   }
33*67e74705SXin Li };
34*67e74705SXin Li 
35*67e74705SXin Li X3<int, Y, &Y::x> x3;
36*67e74705SXin Li 
37*67e74705SXin Li typedef int Y::*IntMember;
38*67e74705SXin Li 
39*67e74705SXin Li template<IntMember Member>
40*67e74705SXin Li struct X4 {
41*67e74705SXin Li   X3<int, Y, Member> member;
42*67e74705SXin Li 
getMemberX443*67e74705SXin Li   int &getMember(Y& y) { return y.*Member; }
44*67e74705SXin Li };
45*67e74705SXin Li 
get_X4(X4<& Y::x> x4,Y & y)46*67e74705SXin Li int &get_X4(X4<&Y::x> x4, Y& y) {
47*67e74705SXin Li   return x4.getMember(y);
48*67e74705SXin Li }
49*67e74705SXin Li 
50*67e74705SXin Li template<IntMember Member>
51*67e74705SXin Li void accept_X4(X4<Member>);
52*67e74705SXin Li 
test_accept_X4(X4<& Y::x> x4)53*67e74705SXin Li void test_accept_X4(X4<&Y::x> x4) {
54*67e74705SXin Li   accept_X4(x4);
55*67e74705SXin Li }
56*67e74705SXin Li 
57*67e74705SXin Li namespace ValueDepMemberPointer {
58*67e74705SXin Li   template <void (*)()> struct instantiate_function {};
59*67e74705SXin Li   template <typename T> struct S {
60*67e74705SXin Li     static void instantiate();
61*67e74705SXin Li     typedef instantiate_function<&S::instantiate> x; // expected-note{{instantiation}}
62*67e74705SXin Li   };
instantiate()63*67e74705SXin Li   template <typename T> void S<T>::instantiate() {
64*67e74705SXin Li     int a[(int)sizeof(T)-42]; // expected-error{{array with a negative size}}
65*67e74705SXin Li   }
66*67e74705SXin Li   S<int> s;
67*67e74705SXin Li }
68*67e74705SXin Li 
69*67e74705SXin Li namespace PR18192 {
70*67e74705SXin Li   struct A { struct { int n; }; };
71*67e74705SXin Li   template<int A::*> struct X {};
72*67e74705SXin Li   constexpr int A::*p = &A::n;
73*67e74705SXin Li   X<p> x; // expected-error{{not a pointer to member constant}}
74*67e74705SXin Li }
75