Skip to content

Commit

Permalink
Merge pull request #174 from elbeno/add-type-traits
Browse files Browse the repository at this point in the history
Add `is_scoped_enum`
  • Loading branch information
lukevalenty authored Nov 22, 2024
2 parents 7cdfadd + 363928c commit c8235ab
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions include/stdx/bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ class bitset {

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

Expand Down
5 changes: 3 additions & 2 deletions include/stdx/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ constexpr auto is_specialization_of()

template <typename E>
constexpr bool is_scoped_enum_v =
std::is_enum_v<E> and
not std::is_convertible_v<E, std::underlying_type_t<E>>;
std::is_enum_v<E> and not std::is_convertible_v<E, underlying_type_t<E>>;
template <typename E>
using is_scoped_enum = std::bool_constant<is_scoped_enum_v<E>>;

template <typename...> struct type_list {};
template <auto...> struct value_list {};
Expand Down
7 changes: 6 additions & 1 deletion test/bitset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ namespace {
enum struct Bits : std::uint8_t { ZERO, ONE, TWO, THREE, MAX };
}

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

TEST_CASE("use bitset with enum struct (place_bits construct)", "[bitset]") {
constexpr auto bs = stdx::bitset<Bits::MAX>{stdx::place_bits, Bits::ZERO};
static_assert(bs.to_natural() == 1);
}

#if __cplusplus >= 202002L
TEST_CASE("construct with a ct_string", "[bitset]") {
using namespace stdx::literals;
Expand Down
5 changes: 5 additions & 0 deletions test/type_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ enum struct E2 {};
} // namespace

TEST_CASE("is_scoped_enum", "[type_traits]") {
static_assert(not stdx::is_scoped_enum_v<int>);
static_assert(not stdx::is_scoped_enum_v<E1>);
static_assert(stdx::is_scoped_enum_v<E2>);

static_assert(not stdx::is_scoped_enum<int>::value);
static_assert(not stdx::is_scoped_enum<E1>::value);
static_assert(stdx::is_scoped_enum<E2>::value);
}

TEST_CASE("type_identity", "[type_traits]") {
Expand Down

0 comments on commit c8235ab

Please sign in to comment.