在這個 SO 答案和這個提案中,我們可以看到一個實作(為了方便起見std::is_specialization_of,包括在下面),它可以檢測一個型別是否是給定模板的特化。
template< class T, template<class...> class Primary >
struct is_specialization_of : false_type;
template< template<class...> class Primary, class... Args >
struct is_specialization_of< Primary<Args...>, Primary> : true_type;
該提案明確表示此型別特征不受繼承影響:
提議的特征只考慮專業化。由于特化與繼承無關,當任何模板引數碰巧通過繼承定義時,特征的結果不受影響。
template< class > struct B { };
template< class T > struct D : B<T> { };
static_assert( is_specialization_of_v< B<int>, B> );
static_assert( is_specialization_of_v< D<int>, D> );
static_assert( not is_specialization_of_v< B<int>, D> );
static_assert( not is_specialization_of_v< D<int>, B> );
有沒有辦法實作一些確實考慮到繼承的東西,表現得像std::derived_from但Base可能是給定模板的任何特化?像這樣說std::derived_from_specialization_of:
template<class> struct A {};
template<class T> struct B : A<T> {};
template<class T> struct C : B<T> {};
static_assert(derived_from_specialization_of< A<int>, A>);
static_assert(derived_from_specialization_of< B<int>, B>);
static_assert(derived_from_specialization_of< C<int>, C>);
static_assert(derived_from_specialization_of< B<int>, A>);
static_assert(derived_from_specialization_of< C<int>, B>);
static_assert(derived_from_specialization_of< C<int>, A>);
它還應該支持具有多個引數和/或引數包的模板:
template<class, class> struct A{};
template<typename...> struct B{};
uj5u.com熱心網友回復:
去_Ugly-fied ROM MSVCSTL:
template <template <class...> class Template, class... Args>
void derived_from_specialization_impl(const Template<Args...>&);
template <class T, template <class...> class Template>
concept derived_from_specialization_of = requires(const T& t) {
derived_from_specialization_impl<Template>(t);
};
其中,我們使用實施博覽會專用型性狀is-derived-from-view-interface從[range.view] / 6。
請注意,這is_specialization_of與 OP 中參考的特征具有相同的限制:它僅適用于具有所有型別引數的模板。
[演示]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/368730.html
上一篇:使用一些鍵和物件陣列迭代物件
