1 // { dg-do assemble  }
2 // { dg-options "" }
3 
4 // GROUPS passed operators
5 // Check that the & operator, when applied to a global function
6 // or member function returns a proper value as long as the context
7 // in which the result of & is used requires a pointer to a specific
8 // unambigous (function-pointer) type.
9 //
10 // This test fails (in test5()) when compiled with g++ 1.34.1.
11 
12 extern "C" int printf (const char *, ...);
13 
14 int function (char c);
15 int function (float f);
16 
17 class base {
18 	int filler;
19 public:
20 	int method (char);
21 	int method (float);
22 };
23 
24 void* vp;
25 
26 typedef int (*ptr_to_func_of_char)(char);
27 typedef int (*ptr_to_func_of_float)(float);
28 typedef int (base::*ptr_to_method_of_char)(char);
29 typedef int (base::*ptr_to_method_of_float)(float);
30 
31 int test2 (void*);
32 int test3 (void*);
33 int test4 (void*);
34 int test5 (void*);
35 
36 base* base_ptr;
37 
fail()38 int fail ()
39 {
40   printf ("FAIL\n");
41   return 1;
42 }
43 
main()44 int main ()
45 {
46 	base_ptr = new base;
47 
48 	ptr_to_func_of_char p0 = &function;
49 	vp = (void*) p0;
50 	if (test2 (vp))
51 		return fail ();
52 	ptr_to_func_of_float p1 = &function;
53 	vp = (void*) p1;
54 	if (test3 (vp))
55 		return fail ();
56 	ptr_to_method_of_char p2 = &base::method;
57 	vp = (void*) p2; // { dg-warning "converting" }
58 	if (test4 (vp))
59 		return fail ();
60 	ptr_to_method_of_float p3 = &base::method;
61 	vp = (void*) p3; // { dg-warning "converting" }
62 	if (test5 (vp))
63 		return fail ();
64 
65 	printf ("PASS\n");
66 	return 0;
67 }
68 
test2(void * vp)69 int test2 (void* vp)
70 {
71 	char ch = 'x';
72 
73 	return (((ptr_to_func_of_char)vp)(ch) !=  9901);
74 }
75 
test3(void * vp)76 int test3 (void* vp)
77 {
78 	float flt = 9.9;
79 
80 	return (((ptr_to_func_of_float)vp)(flt) !=  9902);
81 }
82 
test4(void * vp)83 int test4 (void* vp)
84 {
85 	char ch = 'x';
86 	ptr_to_method_of_char p = (ptr_to_method_of_char) vp; // { dg-error "invalid cast" } bad type conversion
87 
88 	return ((base_ptr->*p)(ch) !=  9904);
89 }
90 
test5(void * vp)91 int test5 (void* vp)
92 {
93 	float flt = 9.9;
94 	ptr_to_method_of_float p = (ptr_to_method_of_float) vp; // { dg-error "invalid cast" } bad type conversion
95 
96 	if ((base_ptr->*p)(flt) !=  9905) {
97 		return 1;
98 	} else
99 		return 0;
100 }
101 
function(char c)102 int function (char c)
103 {
104 	c = c;
105 	return 9901;
106 }
107 
function(float f)108 int function (float f)
109 {
110 	f = f;
111 	return 9902;
112 }
113 
method(char c)114 int base::method (char c)
115 {
116 	c = c;
117 	return 9904;
118 }
119 
method(float f)120 int base::method (float f)
121 {
122 	f = f;
123 	return 9905;
124 }
125