typename 参数的模板专用化是特定模板的任何实例化
Template specialization on typename parameter being any instantiation of a particular template
我有一个类模板Z
,当传递一个类型时,我想专门化一个类型,该类型是特定模板的任何实例化N
:
struct L {
template <typename S> void foo(S &) {/*...*/}
};
template <class T>
struct M {
template <typename S> void foo(S &) {/*...*/}
};
template <class T>
struct N {
template <typename S> void foo(S &) {/*...*/}
};
// I'd like to specialize this for TY==N<anything>
template <typename TX, typename TY>
struct Z {
void bar(TX &tx) { /*...*/ ty->foo(tx); /*...*/ }
TY *ty;
};
由于Z<int, L>
、Z<int, N<int>>
和Z<int, M<int>>
都是有效的用例,因此我无法做任何事情将Z
转换为模板模板,并且当TY
是从N
构建的类时,Z<TX, TY>::bar(TX &)
可能会降低引人注目的复杂性。 有没有办法实现这一目标?
这应该会影响您想要的专业化:
template <typename TX, typename ANY>
struct Z< TX, N<ANY> > {
// ...
};
当第一个参数TX
,第二个参数N<ANY>
时,Z
变得专用。快速说明:
template <typename A> struct N { A a; };
template <typename TX, typename TY>
struct Z { Z () { std::cout << "not special" << std::endl; } };
template <typename TX, typename ANY>
struct Z< TX, N<ANY> > { Z () { std::cout << "special" << std::endl; } };
int main ()
{
Z<int, int> z1;
Z<int, N<int> > z2;
}
输出结果:
not special
special
相关文章:
- 从C++实例化QML
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 如何创建一个空的全局类并在启动时实例化它
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 约束和显式模板实例化
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 对象实例化调用构造函数的次数太多
- 如何使用非默认构造函数实例化模板化类
- 静态数据成员模板专用化的实例化点在哪里
- C++-为任何lambda创建一个实例化桶
- 为什么这些函数模板中的任何一个都与实例化不匹配?
- 是否有任何方法可以避免警告/错误模板实例化回溯
- 是否可以编写C++基类成员函数来实例化从中调用它的任何派生类?
- typename 参数的模板专用化是特定模板的任何实例化
- 是否有任何方法可以避免cpp文件中所有方法的重复类实例化
- 无任何内容的提升函数实例化
- 'make_error_code' 未在此范围内声明,并且在实例化点通过依赖于参数的查找未找到任何声明
- 由于抽象模板arg的实例化,Boost::lambda表达式编译失败.任何解释和/或解决方法
- 为什么编译器尝试实例化一个我实际上没有在任何地方实例化的模板?
- 检查类是否继承自模板的任何模板实例化