Skip to content

Commit b228109

Browse files
committed
[type_traits] add copy_cv
1 parent 3077f46 commit b228109

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

include/itlib/type_traits.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// itlib-type_traits v1.02
1+
// itlib-type_traits v1.03
22
//
33
// Additional helper type traits extending the standard <type_traits>
44
//
@@ -28,6 +28,7 @@
2828
//
2929
// VERSION HISTORY
3030
//
31+
// 1.03 (2025-12-15) Add copy_cv
3132
// 1.02 (2023-11-27) Added is_noop_convertible
3233
// 1.01 (2023-03-10) Added type_identity
3334
// 1.00 (2020-12-28) First pulic release
@@ -89,6 +90,14 @@ struct is_noop_convertible {
8990
;
9091
};
9192

93+
template <typename To, typename From>
94+
struct copy_cv {
95+
using type = typename std::conditional<std::is_const<From>::value,
96+
typename std::conditional<std::is_volatile<From>::value, const volatile To, const To>::type,
97+
typename std::conditional<std::is_volatile<From>::value, volatile To, To>::type
98+
>::type;
99+
};
100+
92101
#if __cplusplus >= 201700
93102
template <template <typename...> class Template, typename Type>
94103
inline constexpr bool is_instantiation_of_v = is_instantiation_of<Template, Type>::value;
@@ -98,6 +107,9 @@ using type_identity_t = typename type_identity<T>::type;
98107

99108
template <typename From, typename To>
100109
inline constexpr bool is_noop_convertible_v = is_noop_convertible<From, To>::value;
110+
111+
template <typename To, typename From>
112+
using copy_cv_t = typename copy_cv<To, From>::type;
101113
#endif
102114

103115
}

test/t-type_traits-11.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,26 @@ TEST_CASE("is_noop_convertible") {
6161
CCHECK_FALSE(itlib::is_noop_convertible<int8_t, bool>::value);
6262
CCHECK_FALSE(itlib::is_noop_convertible<int8_t, const bool>::value);
6363
}
64+
65+
TEST_CASE("copy_cv") {
66+
CCHECK(std::is_same<
67+
itlib::copy_cv<int, const volatile float>::type,
68+
const volatile int
69+
>::value);
70+
CCHECK(std::is_same<
71+
itlib::copy_cv<const int, volatile float>::type,
72+
volatile const int
73+
>::value);
74+
CCHECK(std::is_same<
75+
itlib::copy_cv<volatile int, const float>::type,
76+
const volatile int
77+
>::value);
78+
CCHECK(std::is_same<
79+
itlib::copy_cv<const volatile int, float>::type,
80+
const volatile int
81+
>::value);
82+
CCHECK(std::is_same<
83+
itlib::copy_cv<int, float>::type,
84+
int
85+
>::value);
86+
}

test/t-type_traits-17.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,15 @@ TEST_CASE("is_noop_convertible") {
5353
CCHECK_FALSE(itlib::is_noop_convertible_v<void*, float>);
5454
CCHECK_FALSE(itlib::is_noop_convertible_v<void*, double>);
5555
}
56+
57+
58+
TEST_CASE("copy_cv") {
59+
using T1 = itlib::copy_cv_t<float, const volatile int>;
60+
CCHECK(std::is_same_v<T1, const volatile float>);
61+
using T2 = itlib::copy_cv_t<double, int>;
62+
CCHECK(std::is_same_v<T2, double>);
63+
using T3 = itlib::copy_cv_t<long, volatile char>;
64+
CCHECK(std::is_same_v<T3, volatile long>);
65+
using T4 = itlib::copy_cv_t<unsigned short, const short>;
66+
CCHECK(std::is_same_v<T4, const unsigned short>);
67+
}

0 commit comments

Comments
 (0)