add: [imp] copy_cvref

This commit is contained in:
mutouyun 2023-04-01 18:01:33 +08:00
parent b75022b4a4
commit 43021c1aba
2 changed files with 56 additions and 0 deletions

View File

@ -84,4 +84,42 @@ struct is_specialized : std::false_type {};
template <template <typename...> class Tt, typename... A> template <template <typename...> class Tt, typename... A>
struct is_specialized<Tt, Tt<A...>> : std::true_type {}; struct is_specialized<Tt, Tt<A...>> : std::true_type {};
/**
* \brief Copy the cv qualifier and reference of the source type to the target type.
*/
template <typename Src, typename Des>
struct copy_cvref {
using type = Des;
};
template <typename Src, typename Des>
struct copy_cvref<Src const, Des> {
using type = typename std::add_const<Des>::type;
};
template <typename Src, typename Des>
struct copy_cvref<Src volatile, Des> {
using type = typename std::add_volatile<Des>::type;
};
template <typename Src, typename Des>
struct copy_cvref<Src const volatile, Des> {
using type = typename std::add_cv<Des>::type;
};
template <typename Src, typename Des>
struct copy_cvref<Src &, Des> {
using type = typename std::add_lvalue_reference<
typename copy_cvref<Src, Des>::type>::type;
};
template <typename Src, typename Des>
struct copy_cvref<Src &&, Des> {
using type = typename std::add_rvalue_reference<
typename copy_cvref<Src, Des>::type>::type;
};
template <typename Src, typename Des>
using copy_cvref_t = typename copy_cvref<Src, Des>::type;
LIBIMP_NAMESPACE_END_ LIBIMP_NAMESPACE_END_

View File

@ -134,3 +134,21 @@ TEST(utility, in_place) {
[](std::in_place_t) {}(imp::in_place); [](std::in_place_t) {}(imp::in_place);
} }
#endif/*LIBIMP_CPP_17*/ #endif/*LIBIMP_CPP_17*/
TEST(utility, copy_cvref) {
EXPECT_TRUE((std::is_same<imp::copy_cvref_t<int , long>, long >()));
EXPECT_TRUE((std::is_same<imp::copy_cvref_t<int & , long>, long & >()));
EXPECT_TRUE((std::is_same<imp::copy_cvref_t<int &&, long>, long &&>()));
EXPECT_TRUE((std::is_same<imp::copy_cvref_t<int const , long>, long const >()));
EXPECT_TRUE((std::is_same<imp::copy_cvref_t<int const & , long>, long const & >()));
EXPECT_TRUE((std::is_same<imp::copy_cvref_t<int const &&, long>, long const &&>()));
EXPECT_TRUE((std::is_same<imp::copy_cvref_t<int volatile , long>, long volatile >()));
EXPECT_TRUE((std::is_same<imp::copy_cvref_t<int volatile & , long>, long volatile & >()));
EXPECT_TRUE((std::is_same<imp::copy_cvref_t<int volatile &&, long>, long volatile &&>()));
EXPECT_TRUE((std::is_same<imp::copy_cvref_t<int const volatile , long>, long const volatile >()));
EXPECT_TRUE((std::is_same<imp::copy_cvref_t<int const volatile & , long>, long const volatile & >()));
EXPECT_TRUE((std::is_same<imp::copy_cvref_t<int const volatile &&, long>, long const volatile &&>()));
}