简体   繁体   English

使用声明来表示模板声明中的类型

[英]Using declaration to denote a type within a template declaration

I have the following template declaration: 我有以下模板声明:

template <typename T1, typename T2>
class tree{
public:
tree(int,T2&);
~tree();
...
rNodePtrIter travPreord(int);
void travInord();
rNodePtrIter travInord(int);
void travPostord();
rNodePtrIter travPostord(int);
private:
NodePtrIter hierarchy;
};

I declared the type rNodePtrIter just above the template declaration as follows: 我在模板声明上方声明了rNodePtrIter类型,如下所示:

using rNodePtrIter = list<unique_ptr<node<T1>>>::iterator&;

That doesn't compile as the compiler complains that the type T1 is not known: 由于编译器抱怨类型T1未知,因此无法编译:

error: use of undeclared identifier 'T1' 错误:使用未声明的标识符“ T1”

Is there a way to get in the using declaration as above, so that I could use the alias rNodePtrIter in my template declaration, instead of the longer alternative? 有没有一种获取上述using声明的方法,这样我可以在模板声明中使用别名rNodePtrIter而不是更长的替代方法?

TIA TIA

Vinod 维诺德

You might use alias template : 您可以使用别名模板

template<typename T1>
using rNodePtrIter = typename list<unique_ptr<node<T1>>>::iterator&;

and then replace rNodePtrIter inside the class with rNodePtrIter<T1> . 然后将rNodePtrIter内的rNodePtrIter<T1>替换为rNodePtrIter<T1>

Just move type alias declaration inside of template: 只需在模板内部移动类型别名声明即可:

template <typename T1, typename T2>
class tree{
public: using rNodePtrIter = typename list<unique_ptr<node<T1>>>::iterator&;

Note that it requires node template to be already declared. 请注意,它要求已经声明node模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM