-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>); | ||
} | ||
} |