简体   繁体   中英

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:

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

That doesn't compile as the compiler complains that the type T1 is not known:

error: use of undeclared identifier '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?

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> .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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