在C++中编写类库代码我发现copy_cv_reference_t
类型特征特别需要:
struct A;
struct B;
static_assert(std::is_same< copy_cv_reference_t< A , B >, B >{});
static_assert(std::is_same< copy_cv_reference_t< A const , B >, B const >{});
static_assert(std::is_same< copy_cv_reference_t< volatile A , B >, volatile B >{});
static_assert(std::is_same< copy_cv_reference_t< volatile A const , B >, volatile B const >{});
static_assert(std::is_same< copy_cv_reference_t< A &, B >, B & >{});
static_assert(std::is_same< copy_cv_reference_t< A const &, B >, B const & >{});
static_assert(std::is_same< copy_cv_reference_t< volatile A &, B >, volatile B & >{});
static_assert(std::is_same< copy_cv_reference_t< volatile A const &, B >, volatile B const & >{});
static_assert(std::is_same< copy_cv_reference_t< A &&, B >, B && >{});
static_assert(std::is_same< copy_cv_reference_t< A const &&, B >, B const && >{});
static_assert(std::is_same< copy_cv_reference_t< volatile A &&, B >, volatile B && >{});
static_assert(std::is_same< copy_cv_reference_t< volatile A const &&, B >, volatile B const && >{});
我使用两种方法为自己发明了它:通过类型限定符的 id 和仅通过 SFINAE。
#include <type_traits>
#if 1
enum class type_qual_id
{
value,
const_value,
lref,
const_lref,
rref,
const_rref,
volatile_value,
volatile_const_value,
volatile_lref,
volatile_const_lref,
volatile_rref,
volatile_const_rref,
};
template< type_qual_id tqid, typename type > struct add_type_qualifier;
template< typename to > struct add_type_qualifier< type_qual_id::value , to > { using type = to ; };
template< typename to > struct add_type_qualifier< type_qual_id::const_value , to > { using type = to const ; };
template< typename to > struct add_type_qualifier< type_qual_id::lref , to > { using type = to & ; };
template< typename to > struct add_type_qualifier< type_qual_id::const_lref , to > { using type = to const & ; };
template< typename to > struct add_type_qualifier< type_qual_id::rref , to > { using type = to &&; };
template< typename to > struct add_type_qualifier< type_qual_id::const_rref , to > { using type = to const &&; };
template< typename to > struct add_type_qualifier< type_qual_id::volatile_value , to > { using type = volatile to ; };
template< typename to > struct add_type_qualifier< type_qual_id::volatile_const_value, to > { using type = volatile to const ; };
template< typename to > struct add_type_qualifier< type_qual_id::volatile_lref , to > { using type = volatile to & ; };
template< typename to > struct add_type_qualifier< type_qual_id::volatile_const_lref , to > { using type = volatile to const & ; };
template< typename to > struct add_type_qualifier< type_qual_id::volatile_rref , to > { using type = volatile to &&; };
template< typename to > struct add_type_qualifier< type_qual_id::volatile_const_rref , to > { using type = volatile to const &&; };
template< type_qual_id tqid, typename to >
using add_qualifier_t = typename add_type_qualifier< tqid, to >::type;
template< typename type > constexpr type_qual_id get_type_qualifier_id = type_qual_id::value ;
template< typename type > constexpr type_qual_id get_type_qualifier_id< type const > = type_qual_id::const_value ;
template< typename type > constexpr type_qual_id get_type_qualifier_id< type & > = type_qual_id::lref ;
template< typename type > constexpr type_qual_id get_type_qualifier_id< type const & > = type_qual_id::const_lref ;
template< typename type > constexpr type_qual_id get_type_qualifier_id< type && > = type_qual_id::rref ;
template< typename type > constexpr type_qual_id get_type_qualifier_id< type const && > = type_qual_id::const_rref ;
template< typename type > constexpr type_qual_id get_type_qualifier_id< volatile type > = type_qual_id::volatile_value ;
template< typename type > constexpr type_qual_id get_type_qualifier_id< volatile type const > = type_qual_id::volatile_const_value;
template< typename type > constexpr type_qual_id get_type_qualifier_id< volatile type & > = type_qual_id::volatile_lref ;
template< typename type > constexpr type_qual_id get_type_qualifier_id< volatile type const & > = type_qual_id::volatile_const_lref ;
template< typename type > constexpr type_qual_id get_type_qualifier_id< volatile type && > = type_qual_id::volatile_rref ;
template< typename type > constexpr type_qual_id get_type_qualifier_id< volatile type const && > = type_qual_id::volatile_const_rref ;
template< typename from, typename to >
using copy_cv_reference_t = add_qualifier_t< get_type_qualifier_id< from >, to >;
#else
#include <type_traits>
template< typename from, typename to >
struct copy_cv
{
using type = to;
};
template< typename from, typename to >
struct copy_cv< from const, to >
: copy_cv< from, to const >
{
};
template< typename from, typename to >
struct copy_cv< volatile from, to >
: copy_cv< from, volatile to >
{
};
template< typename from, typename to >
struct copy_cv< volatile from const, to >
: copy_cv< from, volatile to const >
{
};
template< typename from, typename to >
struct copy_reference
{
using type = to;
};
template< typename from, typename to >
struct copy_reference< from &, to >
: copy_reference< from, to & >
{
};
template< typename from, typename to >
struct copy_reference< from &&, to >
: copy_reference< from, to && >
{
};
template< typename from, typename to >
using copy_cv_reference_t = typename copy_reference< from, typename copy_cv< std::remove_reference_t< from >, to >::type >::type;
#endif
第一种方法看起来有点人为,但提供了一个“类型限定符 id”作为附加方面,后者在某些情况下可能很有用。第二种方法本质上是两步法。它可能有缺点。此外,它还涉及std::remove_reference_t
显示 cv 限定类型。
一方面,我知道标准允许实现具有“内在”类型的特征。另一方面,当前C++标准中没有类型特征。
copy_cv_reference_t
类型特征的最佳实现是什么?不仅在以上两者之间。有没有更好的方法来实现它?有没有相应的提议?
取名呢?id的顺序呢?