Skip to content

Commit fd14240

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

File tree

3 files changed

+49
-2
lines changed

3 files changed

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

0 commit comments

Comments
 (0)