1

Description

I declared a template class

template <typename T,size_t RootNum>
class Tree;

And I want to specialize another template class BinaryTree, whose RootNum is 2, but every members is identicial with class Tree.
An inelegant method is defining a class BinaryTree inherits class Tree as below

template <typename T>
class BinaryTree: public Tree<T,2>{};

But I vaguely remember that it exists a declaration simillar to this style to 'elegantly' specialize a template class:

template <typename T>
typedef Tree<T,2> BinaryTree;

Actually the above code is invalid. I wonder is there a keyword-like to concisely achive this operation.

Thank you for your kind suggession.

1 Answers1

3

You are looking for an alias template:

template <typename T>
using BinaryTree = Tree<T,2>;
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Ah, I'm silly. I used to try ```C++ template using BinaryTree = Tree ``` And I faild. I didn't realize it was like that. Thank you very much! –  Aug 19 '22 at 11:26
  • Stackoverflow requires several minutes to allow you accept an answer –  Aug 19 '22 at 12:10