| 1 |
// Examples from P1099R5 |
| 2 |
// { dg-do compile { target c++20 } } |
| 3 |
|
| 4 |
namespace my_lib { |
| 5 |
|
| 6 |
enum class errcode |
| 7 |
{ |
| 8 |
SUCCESS = 0, |
| 9 |
ENOMEM = 1, |
| 10 |
EAGAIN = 2, |
| 11 |
ETOOSLOW = 3 |
| 12 |
}; |
| 13 |
using enum errcode; // import enumerators into namespace |
| 14 |
} |
| 15 |
|
| 16 |
namespace NS { |
| 17 |
my_lib::errcode get_widget() { |
| 18 |
using namespace my_lib; |
| 19 |
return ETOOSLOW; // works, and conversions to int don't. |
| 20 |
int i = ETOOSLOW; // { dg-error "" } |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
enum class rgba_color_channel { red, green, blue, alpha}; |
| 25 |
|
| 26 |
const char * to_string(rgba_color_channel channel) { |
| 27 |
switch (channel) { |
| 28 |
using enum rgba_color_channel; |
| 29 |
case red: return "red"; |
| 30 |
case green: return "green"; |
| 31 |
case blue: return "blue"; |
| 32 |
case alpha: return "alpha"; |
| 33 |
} |
| 34 |
return nullptr; |
| 35 |
} |
| 36 |
|
| 37 |
namespace ns { |
| 38 |
struct E_detail { |
| 39 |
enum E { e1, e2 }; |
| 40 |
friend void swap(E&, E&); // adl-only swap in the only associated scope of the enum |
| 41 |
}; |
| 42 |
using E = E_detail::E; // import E into ns |
| 43 |
using enum E; // expose the enumerators of E in ns. Also note the direct reference to E. |
| 44 |
} |
| 45 |
|
| 46 |
int main() { |
| 47 |
auto x = ns::e1; |
| 48 |
auto y = ns::e2; |
| 49 |
swap(x, y); // finds the swap in the associated struct |
| 50 |
} |
| 51 |
|
| 52 |
namespace N0 { |
| 53 |
enum E { x }; |
| 54 |
struct S { |
| 55 |
enum H { y }; |
| 56 |
enum class K { z }; |
| 57 |
using E::x; // OK, introduces x into S |
| 58 |
using E::x; // { dg-error "" } redeclaration in class scope |
| 59 |
using H::y; // { dg-error "" } redeclaration in class scope |
| 60 |
using K::z; // OK, introduces z into S |
| 61 |
}; |
| 62 |
namespace NS { |
| 63 |
enum H { y }; |
| 64 |
enum class K { z }; |
| 65 |
using E::x; // OK, introduces x into NS |
| 66 |
using E::x; // OK, just a redeclaration of the same entity |
| 67 |
using H::y; // OK, redeclaration of the same entity |
| 68 |
using K::z; // OK, introduces z into NS |
| 69 |
}; |
| 70 |
} |
| 71 |
namespace N1 { |
| 72 |
struct S { |
| 73 |
enum E { x }; |
| 74 |
enum class EC { y }; |
| 75 |
using EC::y; |
| 76 |
}; |
| 77 |
|
| 78 |
void f() { |
| 79 |
using S::x; // OK |
| 80 |
x; // resolves to S::E::x; |
| 81 |
using S::y; // OK |
| 82 |
y; // resolves to S::EC::y; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
namespace N2 { |
| 87 |
enum class E { a, b, c }; |
| 88 |
using E::a, E::b, E::c; // OK, imports all three |
| 89 |
auto x = (a,b,c); |
| 90 |
} |
| 91 |
|
| 92 |
namespace N3 { |
| 93 |
struct B { |
| 94 |
enum class E { x }; |
| 95 |
}; |
| 96 |
enum class H { y }; |
| 97 |
struct C : B { |
| 98 |
using enum B::E; // OK, introduces E::x into C |
| 99 |
using enum H; // OK, introduces y into C. Does not introduce H |
| 100 |
}; |
| 101 |
auto i = C::y; // OK |
| 102 |
C::H h; // { dg-error "" } |
| 103 |
} |
| 104 |
|
| 105 |
namespace N4 { |
| 106 |
enum class button { up, down }; |
| 107 |
struct S { |
| 108 |
using button::up; |
| 109 |
button b = up; // OK |
| 110 |
}; |
| 111 |
} |
| 112 |
|
| 113 |
namespace N5 { |
| 114 |
enum class fruit { orange, apple }; |
| 115 |
struct S { |
| 116 |
using enum fruit; // OK, introduces orange and apple into S |
| 117 |
}; |
| 118 |
void f() { |
| 119 |
S s; |
| 120 |
s.orange; // OK, names fruit::orange |
| 121 |
S::orange; // OK, names fruit::orange |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
namespace N6 { |
| 126 |
enum class fruit { orange, apple }; |
| 127 |
enum class color { red, orange }; |
| 128 |
void f() { |
| 129 |
using enum fruit; // OK |
| 130 |
using enum color; // { dg-error "" } color::orange and fruit::orange conflict |
| 131 |
} |
| 132 |
} |