问题描述
任何人都可以帮助我这个代码。我试图专门化一种方法。目前它不能与一个专业化(1),但我想最终有很多专业(2,3,4,5等)
Can anyone help me with this code. I'm trying to specialise a method. At the moment it doesn't work with one specialisation (1) but I'd like to ultimately have lots of specialisations (2, 3, 4, 5 etc)
class X
{
public:
// declaration
template< int FLD >
void set_native( char *ptr, unsigned int length );
// specialisations
template<> void set_native< 1 >( char *ptr, unsigned int length )
{
}
};
我得到的错误消息是..
The error messages I'm getting are..
x.cpp:13:error:在非命名空间作用域中的显式专门化'class X'
x.cpp:13:error:template-id'set_native' *,unsigned int)'不匹配任何模板声明
x.cpp:13:错误:函数声明无效
x.cpp:13: error: explicit specialization in non-namespace scope 'class X'x.cpp:13: error: template-id 'set_native<1>' for 'void set_native(char*, unsigned int)' does not match any template declarationx.cpp:13: error: invalid function declaration
推荐答案
p>正如Benoit提出的,你必须专门处理周围命名空间中的成员函数:
As Benoit proposed, you have to specialize the member function in the surrounding namespace:
struct X {
template<int N> void f() {}
};
template<> void X::f<1>() {} // explicit specialization at namespace scope
(C ++ 03):
This is because of §14.7.3 (C++03):
$ b $然而,在这方面,VC不符合标准,因此造成一些可移植性的头痛。
VC however doesn't conform to the standard in that regard and thus creates some portability headaches.
这篇关于模板方法专门化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!