Writing library-like code in C++ I found there is particular need in copy_cv_reference_t
type trait:
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 && >{});
I invent it for myself using two approaches: via means of id of type qualifiers and via SFINAE only.
#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
First approach looks like something more artificial, but provides a "type qualifiers id" as additional side and latter can be useful in some situations. Second approach is inherently two-step one. It could has downsides. In addition, it involving std::remove_reference_t
to reveal the cv-qualified type.
On the one hand, I know standard allows for implementations to have an "intrinsic" type traits. On the other hand, there is no the type trait currently in contemporary C++ standard.
What is the best implementation of copy_cv_reference_t
type trait? Not only between above two. Are there better approaches to implement it? Is there corresponding proposal?
What about naming? What about order of ids?
Aucun commentaire:
Enregistrer un commentaire