Skip to content

Commit 7deb24e

Browse files
committed
Add fly::size_of_type_is trait
1 parent 7d7fb84 commit 7deb24e

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

fly/traits/traits.hpp

+10
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ template <typename T, typename A, typename... As>
106106
// NOLINTNEXTLINE(readability-identifier-naming)
107107
inline constexpr bool any_same_v = any_same<T, A, As...>::value;
108108

109+
/**
110+
* Trait for testing if the size of a given type is the provided size.
111+
*/
112+
template <typename T, std::size_t Size>
113+
using size_of_type_is = std::bool_constant<sizeof(T) == Size>;
114+
115+
template <typename T, std::size_t Size>
116+
// NOLINTNEXTLINE(readability-identifier-naming)
117+
inline constexpr bool size_of_type_is_v = size_of_type_is<T, Size>::value;
118+
109119
/**
110120
* Overloaded visitation pattern for std::visit. Allows providing a variadic list of lambdas for
111121
* overload resolution in a call to std::visit. Example:

test/traits/traits.cpp

+17-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ CATCH_TEST_CASE("Traits", "[traits]")
241241
CATCH_CHECK(is_class_or_pointer(&f));
242242
}
243243

244-
CATCH_SECTION("Variadic all_same trait")
244+
CATCH_SECTION("Trait: all_same")
245245
{
246246
CATCH_CHECK(fly::all_same_v<int, int>);
247247
CATCH_CHECK(fly::all_same_v<int, const int>);
@@ -279,7 +279,7 @@ CATCH_TEST_CASE("Traits", "[traits]")
279279
CATCH_CHECK_FALSE(fly::all_same_v<FooClass, FooClass, std::string>);
280280
}
281281

282-
CATCH_SECTION("Variadic any_same trait")
282+
CATCH_SECTION("Trait: any_same")
283283
{
284284
CATCH_CHECK(fly::any_same_v<int, int>);
285285
CATCH_CHECK(fly::any_same_v<int, const int>);
@@ -320,6 +320,21 @@ CATCH_TEST_CASE("Traits", "[traits]")
320320
CATCH_CHECK_FALSE(fly::any_same_v<FooClass, std::string>);
321321
}
322322

323+
CATCH_SECTION("Trait: size_of_type_is")
324+
{
325+
CATCH_CHECK(fly::size_of_type_is_v<int, sizeof(int)>);
326+
CATCH_CHECK(fly::size_of_type_is_v<bool, sizeof(bool)>);
327+
CATCH_CHECK(fly::size_of_type_is_v<FooClass, sizeof(FooClass)>);
328+
329+
CATCH_CHECK_FALSE(fly::size_of_type_is_v<int, sizeof(int) - 1>);
330+
CATCH_CHECK_FALSE(fly::size_of_type_is_v<bool, sizeof(bool) - 1>);
331+
CATCH_CHECK_FALSE(fly::size_of_type_is_v<FooClass, sizeof(FooClass) - 1>);
332+
333+
CATCH_CHECK_FALSE(fly::size_of_type_is_v<int, sizeof(int) + 1>);
334+
CATCH_CHECK_FALSE(fly::size_of_type_is_v<bool, sizeof(bool) + 1>);
335+
CATCH_CHECK_FALSE(fly::size_of_type_is_v<FooClass, sizeof(FooClass) + 1>);
336+
}
337+
323338
CATCH_SECTION("Overloaded visitation pattern")
324339
{
325340
using TestVariant = std::variant<int, bool, std::string>;

0 commit comments

Comments
 (0)