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