Skip to content

Commit b92a23a

Browse files
committed
Add a concept to constrain the size of a type
1 parent b6c5c21 commit b92a23a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

fly/traits/concepts.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "fly/traits/traits.hpp"
4+
35
#include <concepts>
46
#include <type_traits>
57

@@ -29,6 +31,12 @@ concept SameAsAny = (std::same_as<std::remove_cvref_t<T>, std::remove_cvref_t<Ts
2931
template <typename T, typename... Ts>
3032
concept SameAsAll = (std::same_as<std::remove_cvref_t<T>, std::remove_cvref_t<Ts>> && ...);
3133

34+
/**
35+
* Concept that is satisified if the given type is the provided size.
36+
*/
37+
template <typename T, std::size_t Size>
38+
concept SizeOfTypeIs = fly::size_of_type_is_v<T, Size>;
39+
3240
/**
3341
* Concept that is satisified if the given type is a signed integral type.
3442
*

test/traits/concepts.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,19 @@ CATCH_TEST_CASE("Concepts", "[traits]")
137137
CATCH_CHECK_FALSE(fly::FloatingPoint<std::string>);
138138
CATCH_CHECK_FALSE(fly::FloatingPoint<FooClass>);
139139
}
140+
141+
CATCH_SECTION("Concept: SizeOfTypeIs")
142+
{
143+
CATCH_CHECK(fly::SizeOfTypeIs<int, sizeof(int)>);
144+
CATCH_CHECK(fly::SizeOfTypeIs<bool, sizeof(bool)>);
145+
CATCH_CHECK(fly::SizeOfTypeIs<FooClass, sizeof(FooClass)>);
146+
147+
CATCH_CHECK_FALSE(fly::SizeOfTypeIs<int, sizeof(int) - 1>);
148+
CATCH_CHECK_FALSE(fly::SizeOfTypeIs<bool, sizeof(bool) - 1>);
149+
CATCH_CHECK_FALSE(fly::SizeOfTypeIs<FooClass, sizeof(FooClass) - 1>);
150+
151+
CATCH_CHECK_FALSE(fly::SizeOfTypeIs<int, sizeof(int) + 1>);
152+
CATCH_CHECK_FALSE(fly::SizeOfTypeIs<bool, sizeof(bool) + 1>);
153+
CATCH_CHECK_FALSE(fly::SizeOfTypeIs<FooClass, sizeof(FooClass) + 1>);
154+
}
140155
}

0 commit comments

Comments
 (0)