У C++ 23 додали можливість явно вказати обʼєкт класу над яким виконується метод в параметрах не статичних і невіртуальних методів.
struct X {
void foo(this X const& self, int i); // same as void foo(int i) const &;
// void foo(int i) const &; // Error: already declared
void bar(this X self, int i); // pass object by value: makes a copy of “*this”
};
За допомоги цієї функціональності можна оновити використання CRTP.
// a CRTP trait
struct add_postfix_increment {
template<typename Self>
auto operator++(this Self&& self, int) {
auto tmp = self; // Self deduces to "some_type"
++self;
return tmp;
}
};
struct some_type : add_postfix_increment {
some_type& operator++() { ... }
};
Більше почитати про це можна тут:
#cpp #cpp23 #explicit_this #crtp #this #self #template #шаблони #нововведення