【发布时间】:2012-02-10 18:21:18
【问题描述】:
有这个代码:
#include <iostream>
#include <string>
#include <typeinfo>
// template class
template <class U, class X, class T>
class Klasa{
public:
template <class Z>
void Method(){
}
};
// partial class specialization
template <class U>
class Klasa<U, int, U>
{
public:
// template method
template <class Z>
void Method(){
}
};
// error occurs for that!
template <class U>
template <>
void Klasa<U, int, U>::Method<int>(){
}
int main()
{
Klasa<float, int, float> object;
object.Method<float>();
return 0;
}
编译错误:
error: invalid explicit specialization before ‘>’ token
error: enclosing class templates are not explicitly specialized
error: template-id ‘Method<int>’ for ‘void Klasa<U, int, U>::Method()’ does not match any template declaration
我尝试对方法进行专业化
void Klasa<U, int, U>::Method<int>
,但是编译器不接受它。这个方法的特化怎么写?
【问题讨论】:
-
错误很明显——你不能这样做,而只是部分专门化了课程。