summaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.old-deja/g++.brendan/operators4.C
blob: 6408815cdc236cf5c417017c6a78fafd8d716a9b (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
// { dg-do assemble  }
// { dg-options "" }

// GROUPS passed operators
// Check that the & operator, when applied to a global function
// or member function returns a proper value as long as the context
// in which the result of & is used requires a pointer to a specific
// unambigous (function-pointer) type.
//
// This test fails (in test5()) when compiled with g++ 1.34.1.

extern "C" int printf (const char *, ...); 

int function (char c);
int function (float f);

class base {
	int filler;
public:
	int method (char);
	int method (float);
};

void* vp;

typedef int (*ptr_to_func_of_char)(char);
typedef int (*ptr_to_func_of_float)(float);
typedef int (base::*ptr_to_method_of_char)(char);
typedef int (base::*ptr_to_method_of_float)(float);

int test2 (void*);
int test3 (void*);
int test4 (void*);
int test5 (void*);

base* base_ptr;

int fail ()
{
  printf ("FAIL\n");
  return 1;
}

int main ()
{
	base_ptr = new base;

	ptr_to_func_of_char p0 = &function;
	vp = (void*) p0;
	if (test2 (vp))
		return fail ();
	ptr_to_func_of_float p1 = &function;
	vp = (void*) p1;
	if (test3 (vp))
		return fail ();
	ptr_to_method_of_char p2 = &base::method;
	vp = (void*) p2; // { dg-warning "converting" } 
	if (test4 (vp))
		return fail ();
	ptr_to_method_of_float p3 = &base::method;
	vp = (void*) p3; // { dg-warning "converting" } 
	if (test5 (vp))
		return fail ();

	printf ("PASS\n");
	return 0;
}

int test2 (void* vp)
{
	char ch = 'x';

	return (((ptr_to_func_of_char)vp)(ch) !=  9901);
}

int test3 (void* vp)
{
	float flt = 9.9;

	return (((ptr_to_func_of_float)vp)(flt) !=  9902);
}

int test4 (void* vp)
{
	char ch = 'x';
	ptr_to_method_of_char p = (ptr_to_method_of_char) vp; // { dg-error "invalid cast" } bad type conversion

	return ((base_ptr->*p)(ch) !=  9904);
}

int test5 (void* vp)
{
	float flt = 9.9;
	ptr_to_method_of_float p = (ptr_to_method_of_float) vp; // { dg-error "invalid cast" } bad type conversion

	if ((base_ptr->*p)(flt) !=  9905) {
		return 1;
	} else
		return 0;
}

int function (char c)
{
	c = c;
	return 9901;
}

int function (float f)
{
	f = f;
	return 9902;
}

int base::method (char c)
{
	c = c;
	return 9904;
}

int base::method (float f)
{
	f = f;
	return 9905;
}