具有基类模板的类的模板特化? [英] Template specialisation of class with base class template ?!

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

问题描述

您的专业知识将受到赞赏......


这是一般的模板课程;这个类的所有特化都应该

有一个指向同一个模板类型的特化的指针:


模板< typename T,const int N>

struct Base:T

{

typedef Base< T,N>类型;


类型* pNext;

};

但是,如果我想要一个具有基类类型的专业化怎么办?和

p下一个成员,还有一个特定的功能:

// Base的专业化,附加接口

//

模板<>

struct Base< SomeClass,3>

{

void fn(void);

};


....这里没有一般的基类成员Type和pNext。


什么这是正确的做法吗?如果我专门通过派生来代替

的模板专业化,那么除了模板之外我们还需要

派生类型并知道派生类型是什么所谓的;我真的很想

喜欢能够使用专门的模板''特别''

口味。


像往常一样,当有人指出更好的

方法时,我准备好垂头丧气

Tim

解决方案

" Tim Clacy" <无******* @ nospamphaseone.nospamdk>写道...

您的专业知识将受到赞赏......

这是一个普通的模板课程;这个类
的所有特化都应该有一个指向同一个模板类型的特化的指针:

模板< typename T,const int N>
struct Base:T
{
typedef Base< T,N>输入;

输入* pNext;
};

但是,如果我想要一个具有基类Type和
pNext成员的专业化,还有一个特定的功能:

// Base的专业化,附加接口
//
模板<>
struct Base< SomeClass,3>
{
void fn(void);
};

...这个通用基类成员没有Type和pNext。

这里做的正确的是什么?


您必须在那里声明pNext。 ''输入''你已经专业了,

''SomeClass''。所以,在适当的时候添加


SomeClass * pNext;




如果我专门推导而不是''特殊'风味的专业化的模板。


如果你将模板的各个部分分成了什么,那么你可以做到这一点。

留下来和改变了什么。参见Modern C ++ Design和Modern C ++ Design。和基于政策的

编程。

像往常一样,当有人指出一个
更好的方法时,我已经准备好羞愧了。




模板确实会产生一种过于灵活的错觉,而且程序员的参与太少了,这就是它的错觉。你仍然需要自己做很多事情。


Victor


12月3日星期三2003 13:07:54 +0100,Tim Clacy

< no ******* @ nospamphaseone.nospamdk>写道:

您的专业知识将受到赞赏......

这是一个普通的模板课程;这个类的所有特化都应该有一个指向同一个模板类型的特化的指针:

模板< typename T,const int N>
struct Base:T
{
typedef Base< T,N>输入;

输入* pNext;
};

但是,如果我想要一个具有基类Type和
pNext成员的专业化,还有一个特定的功能:

// Base的专业化,附加接口
//
模板<>
struct Base< SomeClass,3>
{
void fn(void);
};

...这个通用基类成员没有Type和pNext。

这里做的正确的是什么?如果我专门通过派生而不是模板专业化,那么除了模板之外,我们还需要派生类型并知道派生类型的调用;我真的希望能够只使用具有特殊味道的专业化模板。

像往常一样,我准备好在羞耻时垂头丧气有人指出更好的方法

Tim




这对你有用吗?


模板< typename T,const int N>

struct Base:T

{

typedef Base< T,N>输入;

类型* pNext;

};


模板< typename T,const int N>

struct Derived:Base< T,N>

{

typedef Derived< T,N>输入;

};


模板<>

struct Derived< SomeClass,3>

{

void fn(void);

};


嗯,真的不确定我的专业化语法是否合适。 ..


Victor Bazarov写道:


< snip>

这里做的正确的是什么?



你必须在那里声明pNext。 ''输入''你已经专业了,
它是''SomeClass''。所以,在适当的时候添加

SomeClass * pNext;




Victor,


嗨。我不想指向某个班级的指针;我想要一个指向

Base< SomeClass,3>的指针。换句话说,基础

的evey模板特化应该有一个指向它自己的另一个实例的指针,非常具体的类型:


模板< typename T,const int N>

struct Base

{

Base< T,N> * pNext;

};


如果我做了明确的专业化,那么我似乎失去了''基础''

模板定义''pNext''。


来解释;有没有办法通过模板添加功能

专业化没有在基本模板中定义的LOOSING功能?

祝你好运

Tim


Your expertise will be appreciated...

Here''s a general templatised class; all specialisations of this class should
have a pointer to a specialisation of the same, templatised type:

template<typename T, const int N>
struct Base : T
{
typedef Base<T, N> Type;

Type* pNext;
};
However, what if I want a specialisation that has the base class Type and
pNext members, but also a specific function:
// Specialisation of Base, with an additional interface
//
template<>
struct Base<SomeClass, 3>
{
void fn(void);
};

....this doesn''t have the general base class members Type and pNext.

What''s the correct thing to do here? If I specialise by derivation instead
of template specialisation, then in addition to the template(s) we''ll need
derived types and to know what the derived type are called; I would really
like to be able to just use the template with specialisations for ''special''
flavours.

As usual, I ready to hang my head in shame when someone points out a better
approach
Tim

解决方案

"Tim Clacy" <no*******@nospamphaseone.nospamdk> wrote...

Your expertise will be appreciated...

Here''s a general templatised class; all specialisations of this class should have a pointer to a specialisation of the same, templatised type:

template<typename T, const int N>
struct Base : T
{
typedef Base<T, N> Type;

Type* pNext;
};
However, what if I want a specialisation that has the base class Type and
pNext members, but also a specific function:
// Specialisation of Base, with an additional interface
//
template<>
struct Base<SomeClass, 3>
{
void fn(void);
};

...this doesn''t have the general base class members Type and pNext.

What''s the correct thing to do here?
You have to declare pNext there. ''Type'' you already specialised,
it is ''SomeClass''. So, add

SomeClass* pNext;

where appropriate.
If I specialise by derivation instead
of template specialisation, then in addition to the template(s) we''ll need
derived types and to know what the derived type are called; I would really
like to be able to just use the template with specialisations for ''special'' flavours.
You can do that if you split the parts of the template into what
stays and what changes. See "Modern C++ Design" and policy-based
programming.
As usual, I ready to hang my head in shame when someone points out a better approach



Templates do create an illusion of too much flexibility and too little
programmer involvement, which is what it is, just an illusion. You still
have to do many things yourself.

Victor


On Wed, 3 Dec 2003 13:07:54 +0100, "Tim Clacy"
<no*******@nospamphaseone.nospamdk> wrote:

Your expertise will be appreciated...

Here''s a general templatised class; all specialisations of this class should
have a pointer to a specialisation of the same, templatised type:

template<typename T, const int N>
struct Base : T
{
typedef Base<T, N> Type;

Type* pNext;
};
However, what if I want a specialisation that has the base class Type and
pNext members, but also a specific function:
// Specialisation of Base, with an additional interface
//
template<>
struct Base<SomeClass, 3>
{
void fn(void);
};

...this doesn''t have the general base class members Type and pNext.

What''s the correct thing to do here? If I specialise by derivation instead
of template specialisation, then in addition to the template(s) we''ll need
derived types and to know what the derived type are called; I would really
like to be able to just use the template with specialisations for ''special''
flavours.

As usual, I ready to hang my head in shame when someone points out a better
approach
Tim



Would this work for you?

template<typename T, const int N>
struct Base : T
{
typedef Base<T, N> Type;
Type* pNext;
};

template< typename T, const int N >
struct Derived : Base<T, N>
{
typedef Derived<T, N> Type;
};

template<>
struct Derived<SomeClass, 3>
{
void fn(void);
};

Hmmm, really not sure whether my specialization syntax is okay...


Victor Bazarov wrote:

<snip>

What''s the correct thing to do here?



You have to declare pNext there. ''Type'' you already specialised,
it is ''SomeClass''. So, add

SomeClass* pNext;

where appropriate.



Victor,

Hi. I don''t want a pointer to some class; I want a pointer to
Base<SomeClass, 3>. In other words, evey template specialisation of Base
should have a pointer to another instance of its own, very specific type:

template<typename T, const int N>
struct Base
{
Base<T, N>* pNext;
};

If I make an explicit specialisation, then I seem to lose the ''Base''
template defined ''pNext''.

To paraphrase; is there a way to ADD functionality by template
specialisation without LOOSING functionality defined in the base template?
Best regards
Tim


这篇关于具有基类模板的类的模板特化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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