假设我有一个这样的模板类:
template < typename TParam >
class Test
{
// content
};
如果它是类模板的特化,我想提取 TParam
的第一个模板参数。像这样的东西:
template < typename TParam >
class Test
{
using TParamInner = TemplateType<TParam>::Type;
// use TParamInner here
};
附加信息:
- 我可以访问所有 C++98。
- 我可以访问 C++11 的一个子集。
- 如果可能的话,我宁愿避免使用 stdlib(假设这是 因为我使用的嵌入式系统没有可用的标准库和/或因为我的内存严重受限)
最佳答案
您可以通过以下方式接近:
template <class >
struct first_template_param;
template <template <class...> class Z, class T, class... Ts>
struct first_template_param<Z<T, Ts...>> {
using type = T;
}
它不会处理 std::array
或任何其他采用非类型模板参数的类模板。但它将处理所有“普通”类模板。然后您可以随时为所有您想要的添加额外的特化:
template <class T, size_t N>
struct first_template_param<std::array<T,N>> {
using type = T;
}
关于c++ - 如何测试类型参数是否为模板类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36086149/