The following code example is taken from the book
C++23 - The Complete Guide
by Nicolai M. Josuttis,
Leanpub, 2026
The code is licensed under a
Creative Commons Attribution 4.0 International License.
// raw code
#include <iostream>
class Base {
public:
// member function with *this:
void mem() {
if constexpr (std::same_as<decltype(this), Base*>) {
std::cout << "Base::mem() compiles for *this of type 'Base'\n";
}
else {
std::cout << "Base::mem() compiles for *this of a DERIVED type\n";
}
}
// member function with deduced object parameter:
void ded(this auto& self) {
if constexpr (std::same_as<std::remove_cvref_t<decltype(self)>, Base>) {
std::cout << "Base::ded() compiles for self of 'Base' type\n";
}
else {
std::cout << "Base::ded() compiles for self of a DERIVED type\n";
}
}
};
class Der : public Base {
};
int main()
{
Der d;
d.mem(); // call member function with implicit object *this
d.ded(); // call member function with explicit object parameter
}