@@ -84,4 +84,65 @@ TEST_SUBMODULE(enums, m) {
84
84
.value (" ONE" , SimpleEnum::THREE)
85
85
.export_values ();
86
86
});
87
+
88
+ // test_enum_scalar
89
+ enum UnscopedUCharEnum : unsigned char {};
90
+ enum class ScopedShortEnum : short {};
91
+ enum class ScopedLongEnum : long {};
92
+ enum UnscopedUInt64Enum : std::uint64_t {};
93
+ static_assert (py::detail::all_of<
94
+ std::is_same<py::enum_<UnscopedUCharEnum>::Scalar, unsigned char >,
95
+ std::is_same<py::enum_<ScopedShortEnum>::Scalar, short >,
96
+ std::is_same<py::enum_<ScopedLongEnum>::Scalar, long >,
97
+ std::is_same<py::enum_<UnscopedUInt64Enum>::Scalar, std::uint64_t >
98
+ >::value, " Error during the deduction of enum's scalar type with normal integer underlying" );
99
+
100
+ // test_enum_scalar_with_char_underlying
101
+ enum class ScopedCharEnum : char { Zero, Positive };
102
+ enum class ScopedWCharEnum : wchar_t { Zero, Positive };
103
+ enum class ScopedChar32Enum : char32_t { Zero, Positive };
104
+ enum class ScopedChar16Enum : char16_t { Zero, Positive };
105
+
106
+ // test the scalar of char type enums according to chapter 'Character types'
107
+ // from https://en.cppreference.com/w/cpp/language/types
108
+ static_assert (py::detail::any_of<
109
+ std::is_same<py::enum_<ScopedCharEnum>::Scalar, signed char >, // e.g. gcc on x86
110
+ std::is_same<py::enum_<ScopedCharEnum>::Scalar, unsigned char > // e.g. arm linux
111
+ >::value, " char should be cast to either signed char or unsigned char" );
112
+ static_assert (
113
+ sizeof (py::enum_<ScopedWCharEnum>::Scalar) == 2 ||
114
+ sizeof (py::enum_<ScopedWCharEnum>::Scalar) == 4
115
+ , " wchar_t should be either 16 bits (Windows) or 32 (everywhere else)" );
116
+ static_assert (py::detail::all_of<
117
+ std::is_same<py::enum_<ScopedChar32Enum>::Scalar, std::uint_least32_t >,
118
+ std::is_same<py::enum_<ScopedChar16Enum>::Scalar, std::uint_least16_t >
119
+ >::value, " char32_t, char16_t (and char8_t)'s size, signedness, and alignment is determined" );
120
+ #if defined(PYBIND11_HAS_U8STRING)
121
+ enum class ScopedChar8Enum : char8_t { Zero, Positive };
122
+ static_assert (std::is_same<py::enum_<ScopedChar8Enum>::Scalar, unsigned char >::value);
123
+ #endif
124
+
125
+ // test_char_underlying_enum
126
+ py::enum_<ScopedCharEnum>(m, " ScopedCharEnum" )
127
+ .value (" Zero" , ScopedCharEnum::Zero)
128
+ .value (" Positive" , ScopedCharEnum::Positive);
129
+ py::enum_<ScopedWCharEnum>(m, " ScopedWCharEnum" )
130
+ .value (" Zero" , ScopedWCharEnum::Zero)
131
+ .value (" Positive" , ScopedWCharEnum::Positive);
132
+ py::enum_<ScopedChar32Enum>(m, " ScopedChar32Enum" )
133
+ .value (" Zero" , ScopedChar32Enum::Zero)
134
+ .value (" Positive" , ScopedChar32Enum::Positive);
135
+ py::enum_<ScopedChar16Enum>(m, " ScopedChar16Enum" )
136
+ .value (" Zero" , ScopedChar16Enum::Zero)
137
+ .value (" Positive" , ScopedChar16Enum::Positive);
138
+
139
+ // test_bool_underlying_enum
140
+ enum class ScopedBoolEnum : bool { FALSE , TRUE };
141
+
142
+ // bool is unsigned (std::is_signed returns false) and 1-byte long, so represented with u8
143
+ static_assert (std::is_same<py::enum_<ScopedBoolEnum>::Scalar, std::uint8_t >::value, " " );
144
+
145
+ py::enum_<ScopedBoolEnum>(m, " ScopedBoolEnum" )
146
+ .value (" FALSE" , ScopedBoolEnum::FALSE )
147
+ .value (" TRUE" , ScopedBoolEnum::TRUE );
87
148
}
0 commit comments