是否可以从模板中检索同一类型的堆叠模板的最内层类型?我想在以下示例中检索 double
类型:
template<typename T>
struct is_a : std::false_type {};
template<typename T>
struct A
{
using type = std::conditional_t<
is_a<T>::value,
T::type, // if it's an A, go deeper
T>; // if not, we're done
};
template<typename T>
struct is_a<A<T>> : std::true_type {};
int main()
{
A<A<A<A<A<double>>>>>::type d = 3.0;
return 0;
}
它的动机是question .另外,我发现了这个 post , 表示它可能与 typename
或 template
关键字放置有关,但我自己无法让它工作。
最佳答案
除非我遗漏了什么,否则我只会部分专门化一个模板以使事情变得更容易
template<typename T>
struct A
{
using type = T;
};
template<typename T>
struct A<A<T>>
{
using type = typename A<T>::type;
};
int main()
{
A<double>::type a = 5.0;
A<A<double>>::type d = 3.0;
A<A<A<double>>>::type c = 9.5;
return 0;
}
关于c++ - 在模板本身中检索最里面的模板类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50344153/