部分模板专门化
partial template specialization
本文关键字:专门化 更新时间:2023-10-16
有一个场景,其中有一个模板类
template<typename X, typename Y>
class Foo
{
typedef Y::NestedType Bar;
int A (Bar thing);
void B();
int C(X that);
// other stuff
};
,然后我希望A()方法有一个不同的行为,当X是一个给定的类型(但B和C可以保持不变,实际的代码实际上有大约10个其他方法,其中一些是相当长的,并受到频繁的调整。所以我宁愿避免做一个完整的类专门化和复制&粘贴完整的类实现)
我继续写:
template<typename T>
int Foo<MyType, T>::A(Bar thing);
但是我的编译器(clang 163.7.1)甚至拒绝将其视为任何类型的模板专门化。
是否有一些语法错误隐藏在我写代码的方式,或者这种编码风格无效的c++ ?不幸的是,即使其他编译器确实支持这种习惯用法,我的公司仍然坚持使用clang。
谢谢你的帮助。
使用重载
template<typename X, typename Y>
class Foo
{
// allows to wrap up the arguments
template<typename, typename>
struct Types { };
typedef Y::NestedType Bar;
int A (Bar thing) {
return AImpl(thing, Types<X, Y>());
}
void B();
int C(X that);
// other stuff
private:
template<typename X1, typename Y1>
int AImpl(Bar thing, Types<X1, Y1>) {
/* generic */
}
template<typename Y1>
int AImpl(Bar thing, Types<SpecificType, Y1>) {
/* special */
}
};
不能部分特化类模板的成员。您所写的将是类模板本身的部分专门化的成员函数A
的定义。
相关文章:
- 是否可以对零模板参数进行模板专门化
- 尝试根据类中 typedef 的存在来专门化模板函数
- 如何基于模板化类的基类专门化成员函数
- 如何为指向复杂值的迭代器专门化算法?
- 专门化模板覆盖函数/避免对象切片
- 我能否根据其运算符()的签名专门化可变参数模板参数
- 如何使用模板化类专门化模板化函数?
- 线程 std::调用未知类型,无法专门化函数错误
- 输入两个不专门化大小的矩阵
- 如何在模板类中专门化赋值运算符?
- 如何专门化容器和枚举的模板
- Clang拒绝类模板的嵌套类仅通过专门化定义的代码是正确的吗
- 取消专门化C++模板参数
- SFINAE的变分变量模板专门化
- 如何在编译时专门化大型模板函数中的小部分
- 如何在c++运算符()中专门化调用模板
- 模板函数,其中一个参数需要专门化,而另一个不需要
- 如何专门化字符串数组的模板?
- 如何专门化模板
- 如何避免为模板化迭代器的每个可能的实例化专门化iterator_traits?