Skip to content

Commit 8223271

Browse files
committed
[type_traits] add concepts
1 parent b228109 commit 8223271

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

include/itlib/type_traits.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
//
2929
// VERSION HISTORY
3030
//
31-
// 1.03 (2025-12-15) Add copy_cv
31+
// 1.03 (2025-12-15) Add copy_cv, add concepts
3232
// 1.02 (2023-11-27) Added is_noop_convertible
3333
// 1.01 (2023-03-10) Added type_identity
3434
// 1.00 (2020-12-28) First pulic release
@@ -112,4 +112,12 @@ template <typename To, typename From>
112112
using copy_cv_t = typename copy_cv<To, From>::type;
113113
#endif
114114

115+
#if __cplusplus >= 202000
116+
template <typename Type, template <typename...> class Template>
117+
concept instantiation_of = is_instantiation_of_v<Template, Type>;
118+
119+
template <typename From, typename To>
120+
concept noop_convertible_to = is_noop_convertible_v<From, To>;
121+
#endif
122+
115123
}

test/t-type_traits-17.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,4 @@ TEST_CASE("copy_cv") {
6464
CCHECK(std::is_same_v<T3, volatile long>);
6565
using T4 = itlib::copy_cv_t<unsigned short, const short>;
6666
CCHECK(std::is_same_v<T4, const unsigned short>);
67-
}
67+
}

test/t-type_traits-20.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <itlib/type_traits.hpp>
2+
#include <doctest/doctest.h>
3+
#include <vector>
4+
#include <string>
5+
6+
template <itlib::instantiation_of<std::vector> T>
7+
int test_instantiation_of(T&) {
8+
return 42;
9+
}
10+
11+
template <itlib::instantiation_of<std::basic_string> T>
12+
int test_instantiation_of(T&) {
13+
return 24;
14+
}
15+
16+
TEST_CASE("instantiation_of concept") {
17+
std::string s;
18+
CHECK(test_instantiation_of(s) == 24);
19+
std::vector<int> v;
20+
CHECK(test_instantiation_of(v) == 42);
21+
}
22+
23+
template <itlib::noop_convertible_to<int> T>
24+
int test_noop_convertible(T) {
25+
return 7;
26+
}
27+
28+
template <itlib::noop_convertible_to<void*> T>
29+
int test_noop_convertible(T) {
30+
return 14;
31+
}
32+
33+
TEST_CASE("noop_convertible_to concept") {
34+
CHECK(test_noop_convertible(5) == 7);
35+
CHECK(test_noop_convertible(size_t(32)) == 14);
36+
}

0 commit comments

Comments
 (0)