I define a tree node

template<typename KeyType,typename ItemType> struct __Node__{ KeyType key; ItemType item; list<__Node__*> childs; }; 

I want to give the structure a pseudonym:

 template<typename KeyType,typename ItemType> using Node = struct __Node__<KeyType, ItemType>; 

But it does not come out. How to use such a mechanism correctly so that something like this happens:

 typedef struct __Node__<KeyType, ItemType> Node<KeyType, ItemType>; 

?

  • And what is your compiler? Here is the documentation: en.cppreference.com/w/cpp/language/type_alias - VladD
  • @ OlegUP, what's the mistake? With g ++ 4.8.2, your example compiles perfectly. - dzhioev
  • use of identifiers with two underscores is UB - Abyx

2 answers 2

That's right. Specify ready types when creating pseudonyms so that the compiler does not climb into the void.

 #include<iostream> #include<list> using namespace std; template<typename KeyType,typename ItemType> class A{ public: KeyType key; ItemType item; list<A*> l; }; typedef A<int, int> myStructInt; // здесь нужно указывать аргументы typedef A<char, char> myStructChar; // и здесь тоже самое typedef A<int, char> myStruct1, myStruct2; // и здесь тоже самое int main(){ return 0; } 
  • Alas, this is not what is needed. - OlegUP
  • Apparently, without a specification for a concrete one, such a mechanism cannot be used ... - OlegUP

In general, the question was asked because instead of for example:

 void myclass::f(struct Node<T>* node) {} 

Write:

 void myclass::f(_Node<T>* node) {} 

but everything works without the struct keyword (at least in the class declaration), I now did this:

.h file:

 using namespace std; template<typename KeyType,typename ItemType> struct Node{ KeyType key; ItemType item; list<shared_ptr<Node> > childs; }; template<typename KeyType,typename ItemType> class Tree { public: typedef Node<KeyType, ItemType> _Node; !!! private: shared_ptr<_Node> root; public: shared_ptr<_Node> find(KeyType key, shared_ptr<_Node> parent=0); ... }; 

.cpp file:

 template<typename KeyType, typename ItemType> shared_ptr<_Node> Tree<KeyType, ItemType>::find(KeyType key, shared_ptr<_Node> parent) {...} 

main.cpp:

 Tree<int, string> tree;