对类模板成员的显式专用化的约束
Constraints on explicit specialization of a member of a class template
根据 [temp.expl.spec]/16:
类模板的成员或成员模板可以显式专用于类模板的给定隐式实例化...
经过一些测试,我发现专用化应该与类模板隐式实例化中的成员匹配,这意味着它们应该是相同的类型。例如
template<class T> struct A {
void f(T);
static T i;
};
template<> void A<int>::f(int); // ok
// template<> void A<int>::f(char); // error
template<> int A<int>::i; // ok
// template<> char A<int>::i; // error
标准在哪里规定了此类约束?
正如叶甫根尼的评论中所指出的:
实例化struct A
对于类型int
,您可以void f(int);
定义一个方法。
如果要实现template<> void A<int>::f(char) { }
–struct A<int>
中没有定义这样的方法。
为此,您可以专注于整个struct A
int
.
另一种方法是(如 user846834 的答案所示(使 quest 中的方法本身成为模板。
示例代码:
#include <iostream>
template <class T>
struct A {
void f(T);
};
template <>
void A<int>::f(int) { std::cout << "void A<int>::f(int) called.n"; }
#if 0 // ERROR
void A<int>::f(char) { std::cout << "void A<int>::f(char) called.n"; }
#endif // 0
template <class T>
struct B {
void f(T);
};
template <>
struct B<int> {
void f(char);
};
void B<int>::f(char) { std::cout << "void B<int>::f(char) called.n"; }
template <class T>
struct C {
template <class U = T>
void f(U) { std::cout << "void C<T>::f(U) called.n"; }
};
template <> template <>
void C<int>::f(char) { std::cout << "void C<int>::f(char) called.n"; }
int main()
{
A<int> a; a.f(0);
B<int> b; b.f(0);
C<int> c; c.f('0');
// done
return 0;
}
输出:
void A<int>::f(int) called.
void B<int>::f(char) called.
void C<int>::f(char) called.
科里鲁的现场演示
在您给出的链接示例中,只有非类型模板参数 (X1, X2
( 被指定为与 T 不同的类型。 并且只有它们可以指定为不同。类型模板参数需要与专用化相同。
template<class T> struct A {
void f(T);
template<class X1> void g1(T, X1);
template<class X2> void g2(T, X2);
void h(T) { }
};
// member template specialization
template<> template<>
void A<int>::g1(int, char); // X1 deduced as char
template<> template<>
void A<int>::g2<char>(int, char); // X2 specified as char
相关文章:
- .cpp和.h文件中的模板专用化声明
- 调用专用模板时出错"no matching function for call to [...]"
- 模板专用化(按容器):value_type
- 函数作为模板参数,是否对返回类型强制约束
- 约束和显式模板实例化
- IpOpt拒绝解决不受约束的问题
- 使用C++模板时表达约束
- 静态数据成员模板专用化的实例化点在哪里
- 特征 3 类的模板专用化
- Visual Studio 2017 不允许我创建 C++ 专用模板
- 如何在 SCIP C++ 接口中获取 MILP 约束矩阵中的系数值
- 字符串化递归的"std::vector<std::vector<...>>"而不使用部分模板函数专用化
- 具有常量引用参数的函数模板专用化
- 使用其他模板的模板专用化
- 使用专用显卡进行 OpenGL 渲染时帧速率较低
- 嵌套模板显式专用化
- 如何检查模板专用化是否是基本模板的子类?
- 受约束的成员函数和显式模板实例化
- C++:部分模板专用化用例
- 对类模板成员的显式专用化的约束