summaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.old-deja/g++.law/cvt7.C
blob: e68adf02106fba27c3c83c34c71a6a2b7948a1ed (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
// { dg-do run  }
// GROUPS passed conversions
// cvt file
// From: krste@icsi.berkeley.edu (Krste Asanovic)
// Date: Mon, 24 Aug 92 19:05:10 PDT
// Message-Id: <9208250205.AA26351@icsib43.ICSI.Berkeley.EDU>
// Subject: implicit int. convs. from member refs. in switch expressions

#include <cstdio>

class A
{
public:
    A(int j) { i = j; }
    A(A& a) { i = a.i; }
    operator int() { return i; }

    void assign(int v) { i = v; }
    int i;
};

class B
{
public:
    B(A& inArg) : in(inArg), out(0) {}

    A& in;
    A out;

    void run();
};

void
B::run()
{
    // I get a "switch quantity not an integer" error for this statement.
    switch (in)
    // Replacing above with "switch (int(in))" removes the error.
    {
    case 0:
        out = 1;
        break;
    default:
        out = 0;
        break;
    }
}

int main()
{
    A a(1);

    A& ar = a;

    A& ar2 = ar;

    // ....but no errors for this switch statement.
    switch (ar2)
    {
    case 1:
	std::printf ("PASS\n");
        break;
    default:
	{ std::printf ("FAIL\n"); return 1; }
    }

    B b(a);
    a.assign(3);
    b.run();
    a.assign(0);
    b.run();

    return 0;
}