模板方法专门化问题 [英] template method specialisation problem

查看:104
本文介绍了模板方法专门化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我这个代码。我试图专门化一种方法。目前它不能与一个专业化(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 declaration x.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):


明确的专门化应在

An explicit specialization shall be declared in the namespace of which the template is a member, or, for member templates, in the namespace of which the enclosing class or enclosing class template is a member.





















$ b $然而,在这方面,VC不符合标准,因此造成一些可移植性的头痛。

VC however doesn't conform to the standard in that regard and thus creates some portability headaches.

这篇关于模板方法专门化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆