Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ name current type inside template

It is common practice to declarate using/typedef of current type inside a template class:

template <typename T1, typename T2 = std::vector<T1>, typename Allocator>
class my_class {
  //...
public:
  using my_class_t = my_class<T1, T2, Allocator>;  //1
  my_class_t operator()();  //2
  //...
}

I've noticed on my own that it is very comfortable solution to write that using(1) to prevent re-writing complicated type every time one need to - e.g. 2 shows that it can be done easier a lot. My questions:

  • Is there any documented convention what apriopriate name for that type is (type, my_class_t, ...)?
  • Is there any method to easify that declaration (something like pre-defined this_t, maybe in new standards)?
like image 307
Mariusz Jaskółka Avatar asked May 23 '26 02:05

Mariusz Jaskółka


1 Answers

I'm not aware of any convention providing a name of a class inside the class itself. Since anybody external to the class definition would need to spell out the type somehow before being able to access the nested the nested name it seems not necessary for external users.

For the purpose of the class definition itself it may be useful. There exists a short form to access the instantiation within the class template definition or within member definitions: you can just use the name of the template. That is, your using alias could have been written as

using my_class_t = my_class;
like image 174
Dietmar Kühl Avatar answered May 25 '26 16:05

Dietmar Kühl