1 | #include <cstdarg> |
2 | #include <cstdlib> |
3 | #include "ns.h" |
4 | |
5 | namespace { |
6 | typedef unsigned int my_uint_t; |
7 | int i; // Find the line number for anonymous namespace variable i. |
8 | |
9 | int myanonfunc (int a) |
10 | { |
11 | return a + a; |
12 | } |
13 | |
14 | int |
15 | variadic_sum (int arg_count...) |
16 | { |
17 | int sum = 0; |
18 | std::va_list args; |
19 | va_start(args, arg_count); |
20 | |
21 | for (int i = 0; i < arg_count; i++) |
22 | sum += va_arg(args, int); |
23 | |
24 | va_end(args); |
25 | return sum; |
26 | } |
27 | } |
28 | |
29 | namespace A { |
30 | typedef unsigned int uint_t; |
31 | namespace B { |
32 | typedef unsigned int uint_t; |
33 | int j; // Find the line number for named namespace variable j. |
34 | int myfunc (int a); |
35 | int myfunc2(int a) |
36 | { |
37 | return a + 2; |
38 | } |
39 | float myfunc (float f) |
40 | { |
41 | return f - 2.0; |
42 | } |
43 | } |
44 | } |
45 | |
46 | namespace Y |
47 | { |
48 | typedef unsigned int uint_t; |
49 | using A::B::j; |
50 | int foo; |
51 | } |
52 | |
53 | using A::B::j; // using declaration |
54 | |
55 | namespace Foo = A::B; // namespace alias |
56 | |
57 | using Foo::myfunc; // using declaration |
58 | |
59 | using namespace Foo; // using directive |
60 | |
61 | namespace A { |
62 | namespace B { |
63 | using namespace Y; |
64 | int k; |
65 | } |
66 | } |
67 | |
68 | namespace ns1 { |
69 | int value = 100; |
70 | } |
71 | |
72 | namespace ns2 { |
73 | int value = 200; |
74 | } |
75 | |
76 | void test_namespace_scopes() { |
77 | do { |
78 | using namespace ns1; |
79 | printf(format: "ns1::value = %d\n" , value); // Evaluate ns1::value |
80 | } while(0); |
81 | |
82 | do { |
83 | using namespace ns2; |
84 | printf(format: "ns2::value = %d\n" , value); // Evaluate ns2::value |
85 | } while(0); |
86 | } |
87 | |
88 | int Foo::myfunc(int a) |
89 | { |
90 | test_namespace_scopes(); |
91 | |
92 | ::my_uint_t anon_uint = 0; |
93 | A::uint_t a_uint = 1; |
94 | B::uint_t b_uint = 2; |
95 | Y::uint_t y_uint = 3; |
96 | i = 3; |
97 | j = 4; |
98 | printf(format: "::i=%d\n" , ::i); |
99 | printf(format: "A::B::j=%d\n" , A::B::j); |
100 | printf(format: "variadic_sum=%d\n" , variadic_sum(arg_count: 3, 1, 2, 3)); |
101 | myanonfunc(a: 3); |
102 | return myfunc2(a: 3) + j + i + a + 2 + anon_uint + a_uint + b_uint + y_uint; // Set break point at this line. |
103 | } |
104 | |
105 | namespace B { |
106 | struct Bar { |
107 | int x() { return 42; } |
108 | }; |
109 | Bar bar; |
110 | } // namespace B |
111 | |
112 | namespace A::B { |
113 | struct Bar { |
114 | int y() { return 137; } |
115 | }; |
116 | } // namespace A::B |
117 | |
118 | namespace NS1::NS2 { |
119 | struct Foo { |
120 | int bar() { return -2; } |
121 | }; |
122 | } // namespace NS1::NS2 |
123 | |
124 | namespace NS2 { |
125 | struct Foo { |
126 | int bar() { return -3; } |
127 | }; |
128 | } // namespace NS2 |
129 | |
130 | int |
131 | main (int argc, char const *argv[]) |
132 | { |
133 | test_lookup_at_global_scope(); |
134 | test_lookup_at_file_scope(); |
135 | A::test_lookup_at_ns_scope(); |
136 | A::B::test_lookup_at_nested_ns_scope(); |
137 | A::B::test_lookup_at_nested_ns_scope_after_using(); |
138 | test_lookup_before_using_directive(); |
139 | test_lookup_after_using_directive(); |
140 | ::B::Bar bb; |
141 | A::B::Bar ab; |
142 | return Foo::myfunc(a: 12) + bb.x() + ab.y() + NS1::NS2::Foo{}.bar() + |
143 | NS2::Foo{}.bar(); |
144 | } |
145 | |