Skip to content

Commit c8235ab

Browse files
authored
Merge pull request intel#174 from elbeno/add-type-traits
Add `is_scoped_enum`
2 parents 7cdfadd + 363928c commit c8235ab

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

include/stdx/bitset.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ class bitset {
135135

136136
template <typename... Bs>
137137
constexpr explicit bitset(place_bits_t, Bs... bs) {
138-
static_assert((std::is_integral_v<Bs> and ...),
139-
"Bit places must be integral!");
138+
static_assert(((std::is_integral_v<Bs> or std::is_enum_v<Bs>) and ...),
139+
"Bit places must be integral or enumeration types!");
140140
(set(static_cast<std::size_t>(bs)), ...);
141141
}
142142

include/stdx/type_traits.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ constexpr auto is_specialization_of()
114114

115115
template <typename E>
116116
constexpr bool is_scoped_enum_v =
117-
std::is_enum_v<E> and
118-
not std::is_convertible_v<E, std::underlying_type_t<E>>;
117+
std::is_enum_v<E> and not std::is_convertible_v<E, underlying_type_t<E>>;
118+
template <typename E>
119+
using is_scoped_enum = std::bool_constant<is_scoped_enum_v<E>>;
119120

120121
template <typename...> struct type_list {};
121122
template <auto...> struct value_list {};

test/bitset.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ namespace {
427427
enum struct Bits : std::uint8_t { ZERO, ONE, TWO, THREE, MAX };
428428
}
429429

430-
TEST_CASE("use bitset with enum struct (construct)", "[bitset]") {
430+
TEST_CASE("use bitset with enum struct (default construct)", "[bitset]") {
431431
constexpr auto bs = stdx::bitset<Bits::MAX>{};
432432
static_assert(bs.size() == stdx::to_underlying(Bits::MAX));
433433
}
@@ -455,6 +455,11 @@ TEST_CASE("use bitset with enum struct (read index)", "[bitset]") {
455455
static_assert(bs[Bits::THREE]);
456456
}
457457

458+
TEST_CASE("use bitset with enum struct (place_bits construct)", "[bitset]") {
459+
constexpr auto bs = stdx::bitset<Bits::MAX>{stdx::place_bits, Bits::ZERO};
460+
static_assert(bs.to_natural() == 1);
461+
}
462+
458463
#if __cplusplus >= 202002L
459464
TEST_CASE("construct with a ct_string", "[bitset]") {
460465
using namespace stdx::literals;

test/type_traits.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ enum struct E2 {};
7777
} // namespace
7878

7979
TEST_CASE("is_scoped_enum", "[type_traits]") {
80+
static_assert(not stdx::is_scoped_enum_v<int>);
8081
static_assert(not stdx::is_scoped_enum_v<E1>);
8182
static_assert(stdx::is_scoped_enum_v<E2>);
83+
84+
static_assert(not stdx::is_scoped_enum<int>::value);
85+
static_assert(not stdx::is_scoped_enum<E1>::value);
86+
static_assert(stdx::is_scoped_enum<E2>::value);
8287
}
8388

8489
TEST_CASE("type_identity", "[type_traits]") {

0 commit comments

Comments
 (0)