1 // { dg-do run } 2 3 // Copyright 2002 Free Software Foundation 4 // Contributed by Jason Merrill and Alexandre Oliva 5 6 // Test zero-initialization of pointers to data members. Their NULL 7 // value is represented with -1, not 0. 8 9 #include <stdlib.h> 10 11 struct A 12 { 13 int i; 14 }; 15 16 int A::* gp; 17 18 typedef int A::* iApm; 19 20 iApm gp_zero = 0; 21 iApm gp_dflt = iApm(); 22 iApm gp_cast = (iApm)0; 23 iApm gp_func = iApm(0); 24 iApm gp_stat = static_cast<iApm>(0); 25 26 struct AD : A {}; 27 28 int AD::* gp_impl = gp_dflt; 29 int AD::* gp_down = static_cast<int AD::*>(gp_stat); 30 31 int A::* ga[2]; 32 33 // Test use in a simple struct. 34 struct B 35 { 36 int A::* mp; 37 }; 38 39 B gb; 40 41 struct D; 42 struct C; 43 extern D gd; 44 extern C gc; 45 46 // Test that in a class with a constructor, the pointer to member is 47 // zero-initialized until the constructor is run. 48 struct C 49 { 50 int A::* mp; 51 inline C (); 52 }; 53 54 int fail; 55 struct D 56 { 57 int count; 58 inline D (); 59 }; 60 C()61C::C() : mp (&A::i) { gd.count++; } 62 D()63D::D() : count (0) 64 { 65 if (gc.mp != 0) 66 abort (); 67 } 68 69 // The D must come first for this to work. 70 D gd; 71 C gc; 72 main()73int main() 74 { 75 static int A::* slp; 76 static int A::* sla[2]; 77 static B slb; 78 79 if (gp != 0 || slp != 0 80 || gp_zero != 0 || gp_dflt != 0 || gp_cast != 0 81 || gp_func != 0 || gp_stat != 0 82 || gp_impl != 0 || gp_down != 0) 83 abort (); 84 if (ga[1] != 0 || sla[1] != 0) 85 abort (); 86 if (gb.mp != 0 || slb.mp != 0) 87 abort (); 88 } 89