根据 C++ 标准的定义实现 `is_similar` 类型特征

Implementing an `is_similar` type trait based on the definition of the C++ standard

我正在尝试根据标准给出的定义(详情here)实现is_similar类型特征:

Two types T1 and T2 are similar if they have cv-decompositions with the same n such that corresponding Pi components are either the same or one is "array of Ni" and the other is "array of unknown bound of", and the types denoted by U are the same.

但是,我不确定是否完全理解它的含义以及如何实现它:

template <class T, class U, class = void>
struct is_similar_base: std::false_type {};

template <class T, class U>
struct is_similar_base<T, U, /* ??? */ >: std::true_type {};

template <class T, class U>
struct is_similar: is_similar_base<T, U> {};

template <class T, class U>
inline constexpr bool is_similar_v = is_similar<T, U>::value;

欢迎任何帮助。

cv-decomposition 将类型“剥离”为 const/volatile 限定符和 pointer/array 间接的交替层。如果 pointer/array 间接寻址相同(允许“未知边界数组”匹配“n 数组”)并且下面的类型相同(忽略 cv 限定符),则类型相似。

所以,

// every type is similar to itself
template<typename T, typename U>
struct is_similar_impl : std::is_same<T, U> { };
// we don't care about cv-qualifiers
template<typename T, typename U>
struct is_similar : is_similar_impl<std::remove_cv_t<T>, std::remove_cv_t<U>> { };
// peeling off different kinds of pointers/arrays
template<typename T, typename U>
struct is_similar_impl<T*, U*> : is_similar<T, U> { };
template<typename C, typename T, typename U>
struct is_similar_impl<T C::*, U C::*> : is_similar<T, U> { };
template<std::size_t N, typename T, typename U>
struct is_similar_impl<T[N], U[N]> : is_similar<T, U> { };
template<std::size_t N, typename T, typename U>
struct is_similar_impl<T[N], U[]> : is_similar<T, U> { };
template<std::size_t N, typename T, typename U>
struct is_similar_impl<T[], U[N]> : is_similar<T, U> { };

template<typename T, typename U>
constexpr inline bool is_similar_v = is_similar<T, U>::value;

Godbolt and test cases