递归类型模板 [英] recursive type template

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

问题描述

我有以下树的定义:


模板< typename T>

struct tree {

T first;

vector< tree< T second; //分支

};


成功编译。我想要做的是使用

另一个模板,比如''pair'',因为我可能已经在''中定义了一堆有用的

方法' 'pair'':


模板< typename T>

struct tree:public pair< T,tree< T {};


这不编译。我尝试了一些没有

成功的前瞻声明。有什么办法吗?

I have the following tree definition:

template<typename T>
struct tree {
T first;
vector<tree<T second; // branches
};

which compiles successfully. What I''d like to do though is to use
another template like ''pair'', because I might have a bunch of useful
methods already defined in ''pair'':

template<typename T>
struct tree : public pair<T, tree<T {};

This doesn''t compile. I tried some forward declarations with no
success. Is there any way to do it?

推荐答案

n。************ @ gmail.com 写道:
n.************@gmail.com wrote:

我有以下树的定义:


模板< typename T>

struct tree {

T first;

vector< tree< T second; //分支

};


成功编译。
I have the following tree definition:

template<typename T>
struct tree {
T first;
vector<tree<T second; // branches
};

which compiles successfully.



也许,但程序有未定义的行为:标准模板可能只有
用完整类型实例化。它可能不会在库中的std :: vector的实现中显示,但在其他库中可能会失败。

不能保证失败的原因是std :: vector的直接实现仅在内部使用T *。因此,你必须额外检查
来检测不完整的类型。

std :: vector的一些实现就是这样做的。

Maybe, but the program has undefined behavior: standard templates may only
be instantiated with complete types. It may not show in the implementation
of std::vector in your library, but it might fail in others. The reason
that it is not guaranteed to fail is that the straight forward
implementation of std::vector only uses T* internally. Thus, you have to
put in an extra check to detect incomplete types. Some implementations of
std::vector do that.


我想做的就是使用

另一个模板,比如''pair'',因为我可能已经在''pair'中定义了一堆有用的

方法:


模板< typename T>

struct tree:public pair< T,tree< T {};


这不编译。我尝试了一些没有

成功的前瞻声明。有什么办法吗?
What I''d like to do though is to use
another template like ''pair'', because I might have a bunch of useful
methods already defined in ''pair'':

template<typename T>
struct tree : public pair<T, tree<T {};

This doesn''t compile. I tried some forward declarations with no
success. Is there any way to do it?



不是。你可以这样做:


#include< utility>


template< typename T>

struct tree:public std :: pair< T,树< T> * {};


int main(){


tree< inta;

}

最好


Kai-Uwe Bux

Not really. You could do:

#include <utility>

template < typename T >
struct tree : public std::pair< T, tree<T>* {};

int main () {

tree<inta;

}
Best

Kai-Uwe Bux


< n。************ @ gmail.com写信息

新闻:11 ***************** *****@b28g2000cwb.googlegr oups.com ...
<n.************@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...

我有以下树的定义:


模板< typename T>

struct tree {

T first;

vector< tree< T second; //分支

};


成功编译。我想要做的是使用

另一个模板,比如''pair'',因为我可能已经在''中定义了一堆有用的

方法' 'pair'':


模板< typename T>

struct tree:public pair< T,tree< T {};


这不编译。我尝试了一些没有

成功的前瞻声明。有什么办法吗?
I have the following tree definition:

template<typename T>
struct tree {
T first;
vector<tree<T second; // branches
};

which compiles successfully. What I''d like to do though is to use
another template like ''pair'', because I might have a bunch of useful
methods already defined in ''pair'':

template<typename T>
struct tree : public pair<T, tree<T {};

This doesn''t compile. I tried some forward declarations with no
success. Is there any way to do it?



如果你的''对''真的是std :: pair,那么这个对有一个传递给它的每种类型的实例

成员(我认为它确实如此),那么你已经有效地将结构树作为一个成员,这是
是不可能的(因为该成员有另一个自己作为成员的实例,广告infinitum),并且在它的基类中启动,这是更不可能的(???)。这个问题与

模板无关。


呃,你确定''tree''是一种''对' ''?


DW

If your ''pair'' is really std::pair, and that pair has an instance of each type passed to it as a
member (I assume it does), then you''ve effectively got struct tree having itself as a member, which
is impossible (since that member has another instance of itself as a member, ad infinitum), and in
its base class to boot, which is even more impossible (???). This problem isn''t anything to do with
templates.

Uh, are you sure that ''tree'' is a kind of ''pair''?

DW




n.torrey.pi ... @ gmail。 com写道:

n.torrey.pi...@gmail.com wrote:

我有以下树的定义:


模板< typename T>

struct tree {

T first;

vector< tree< T second; //分支

};


成功编译。我想要做的是使用

另一个模板,比如''pair'',因为我可能已经在''中定义了一堆有用的

方法' 'pair'':


模板< typename T>

struct tree:public pair< T,tree< T {};
I have the following tree definition:

template<typename T>
struct tree {
T first;
vector<tree<T second; // branches
};

which compiles successfully. What I''d like to do though is to use
another template like ''pair'', because I might have a bunch of useful
methods already defined in ''pair'':

template<typename T>
struct tree : public pair<T, tree<T {};



tree< Ttemplate类在

被称为基类时没有完全定义。


不确定你要做什么,但尝试使用CRTP。

在基地拥有一些你需要的功能然后通过

派生为Base的templare参数,在你在Base中定义的

std ::对中使用它。

tree<Ttemplate class is not completely defined at the point at which
is being referred as the base class.

Not sure what you are trying to do, but try using CRTP.
Have some of the functionality you need in the base and then pass the
Derived as the templare parameter to the Base, to use it in the
std::pair that you define in the Base.


>

这不编译。我尝试了一些没有

成功的前瞻声明。有什么办法吗?
>
This doesn''t compile. I tried some forward declarations with no
success. Is there any way to do it?


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

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