Skip to content

Commit

Permalink
Feature: is_zst (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
liss-h authored Sep 24, 2024
1 parent 83b108d commit 585408d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ Like `std::variant` but specifically optimized for usage with two types/variants
The internal representation is a `union` of the two types plus a 1 byte (3 state) discriminant.
Additionally, `visit` does not involve any virtual function calls.

### `type_traits.hpp`
Things that are missing in the standard library `<type_traits>` header.

### Further Examples

Compilable code examples can be found in [examples](./examples). The example build requires the cmake
Expand Down
36 changes: 36 additions & 0 deletions include/dice/template-library/type_traits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef DICE_TEMPLATELIBRARY_ZST_HPP
#define DICE_TEMPLATELIBRARY_ZST_HPP

#include <type_traits>

namespace dice::template_library {
namespace detail_zst {
template<typename T>
struct zst_checker {
uint64_t pad;
[[no_unique_address]] T value;

static constexpr bool check() noexcept {
return sizeof(zst_checker) == sizeof(uint64_t);
}
};
} // namespace detail_zst

/**
* Determine if the provided type is a ZST (zero-sized type).
* I.e. a type that can benefit from EBO (empty-base-optimization) or [[no_unique_address]].
* Note: this is not the same as comparing sizeof(T) to 0, types never have a size of 0
*/
template<typename T>
struct is_zst : std::bool_constant<detail_zst::zst_checker<T>::check()> {
};

template<>
struct is_zst<void> : std::false_type {};

template<typename T>
inline constexpr bool is_zst_v = is_zst<T>::value;

} // namespace dice::template_library

#endif // DICE_TEMPLATELIBRARY_ZST_HPP
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ custom_add_test(tests_channel)

add_executable(tests_variant2 tests_variant2.cpp)
custom_add_test(tests_variant2)

add_executable(tests_type_traits tests_type_traits.cpp)
custom_add_test(tests_type_traits)
23 changes: 23 additions & 0 deletions tests/tests_type_traits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

#include <dice/template-library/type_traits.hpp>

TEST_SUITE("type_traits") {

TEST_CASE("is_zst") {
struct zst {};
struct non_zst {
int x;
};

static_assert(dice::template_library::is_zst<zst>::value);
static_assert(dice::template_library::is_zst_v<zst>);

static_assert(!dice::template_library::is_zst<non_zst>::value);
static_assert(!dice::template_library::is_zst_v<non_zst>);

static_assert(!dice::template_library::is_zst<void>::value);
static_assert(!dice::template_library::is_zst_v<void>);
}
}

0 comments on commit 585408d

Please sign in to comment.