您可以使用類模板,像這樣一對夫婦專門化:
template<typename T>
struct RemDec {
using type = T;
};
template<template<typename...> class C, typename... T>
struct RemDec<C<T...>> {
using type = C<typename RemDec<T>::type...>;
};
template<typename T>
struct RemDec<Decorator<T>> {
using type = typename RemDec<T>::type;
};
類模板有助於停止迭代在你的類型的鏈條。
第一個專業記憶一個類模板,並幫助清理遺留物。
最後一個專業化將刪除檢測到的Decorator
,並繼續分析剩下的內容。
它遵循最小,工作示例:
#include<type_traits>
template<typename>
struct Decorator {};
template<typename...>
struct S {};
template<typename T>
struct RemDec {
using type = T;
};
template<template<typename...> class C, typename... T>
struct RemDec<C<T...>> {
using type = C<typename RemDec<T>::type...>;
};
template<typename T>
struct RemDec<Decorator<T>> {
using type = typename RemDec<T>::type;
};
int main() {
static_assert(std::is_same<
typename RemDec<S<Decorator<S<S<Decorator<S<int>>>>>, Decorator<S<double>>>>::type,
S<S<S<S<int>>>, S<double>>
>::value, "!");
}
正如你可以運行它看,任何實例Decorator
是從原始類型刪除。
你的確有一個很好的觀點。 – Lezkus