'递归'模板问题 [英] 'recursive' template problem

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

问题描述




我以一种可实现的方式设计一些模板类有一点问题。

我有一些Container类和一些Proxy类(代理

元素)。看起来像这样:


// P是代理类。

模板< typename T,typename P>

class容器

{

P

whatever(){return P(); }

};


模板< typename T>

类Proxy1 {...};


模板< typename T>

类Proxy2 {...};


由于某种原因,每个类Proxy代理需要要知道,它用于哪个容器

类。 Proxy的某些行为取决于特定的

容器。所以,当我调用方法时()我想提供信息

我在Container< T,P>里面。但是我不能通过另一个模板参数向Proxy类提供

信息,因为我最终会在一个

无限递归定义中,因为Container是由参数化的

代理。我有问题吗?


祝你好运,

alex

Hi,

I have a little problem to design some template classes in a realizable way.
I have some Container classes and some Proxy classes (proxies for
elements). Looks like this:

// P is the Proxy class.
template <typename T, typename P>
class Container
{
P
whatever() { return P(); }
};

template <typename T>
class Proxy1 { ... };

template <typename T>
class Proxy2 { ... };

For some reason every instance of class Proxy needs to know, which Container
class it is used for. Some behavior of Proxy is dependend on the specific
Container. So, when I call method whatever() I want to give the information
that I''m inside Container<T, P> to P. But I cannot give the Proxy class the
information via another template parameter, cause I would end up in an
endless recursive definition, since Container is parameterized by the
Proxy. Got my problem?

Best regards,
alex

推荐答案

亚历山大Stippler写道:
Alexander Stippler wrote:
由于某种原因,类Proxy的每个实例都需要知道,它用于哪个Container类。 Proxy的某些行为取决于特定的
容器。所以,当我调用方法时()我想提供信息
我在Container< T,P>里面。但是我无法通过另一个模板参数向Proxy类提供
信息,因为我最终会进入无限递归定义,因为Container由
代理参数化。我遇到了什么问题?
For some reason every instance of class Proxy needs to know, which Container
class it is used for. Some behavior of Proxy is dependend on the specific
Container. So, when I call method whatever() I want to give the information
that I''m inside Container<T, P> to P. But I cannot give the Proxy class the
information via another template parameter, cause I would end up in an
endless recursive definition, since Container is parameterized by the
Proxy. Got my problem?




出了什么问题:

模板< typename T,模板< typename> P类>

类容器

{

typedef容器< T,P>自我;

P< Self>





因为你不能把这个类型写成一个表达式没有

表示它不是有效类型。



What''s wrong with:
template <typename T, template <typename> class P>
class Container
{
typedef Container<T,P> Self;
P<Self>
etc
?
Just because you cannot write the type as a single expression does not
mean it is not a valid type.


Marc写道:
Marc wrote:
什么错了:
模板< typename T,模板< typename> class P>
class Container
{
typedef Container< T,P>自我;
P< Self>


只是因为你不能把类型写成单个表达式并不意味着它不是一个有效的类型。
What''s wrong with:
template <typename T, template <typename> class P>
class Container
{
typedef Container<T,P> Self;
P<Self>
etc
?
Just because you cannot write the type as a single expression does not
mean it is not a valid type.




谢谢。看起来简单而正确。但我猜P的默认值不是这样可以实现的价值,是吗?


问候,

alex



Thanks. Looks simple and correct. But I guess a default value for P is not
realizable this way, is it?

regards,
alex


Alexander Stippler写道:
Alexander Stippler wrote:
Marc写道:
Marc wrote:
有什么问题:
模板< typename T,模板< typename> class P>
class Container
{
typedef Container< T,P>自我;
P< Self>


只是因为你不能把类型写成单个表达式并不意味着它不是一个有效的类型。
What''s wrong with:
template <typename T, template <typename> class P>
class Container
{
typedef Container<T,P> Self;
P<Self>
etc
?
Just because you cannot write the type as a single expression does not
mean it is not a valid type.



谢谢。看起来简单而正确。但我猜P的默认值不是这样可以实现的,是吗?

问候,
alex



Thanks. Looks simple and correct. But I guess a default value for P is not
realizable this way, is it?

regards,
alex




我有些疑惑。如何实例化这样的类?


容器< double,代理<容器< double,....



I get some doubts. How do I instantiate such a class?

Container<double, Proxy<Container<double, ....


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

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