How does a C++23 explicit object parameter (deducing this) replace CRTP, and where does it behave differently from it?
answer
- the object becomes a parameter
- this Self&& self
- deduced from the call's object expression
- still static, never virtual
basics
~20 sIn C++23 a member function template can take its object as a deduced parameter, template <class Self> auto f(this Self&& self), so Self becomes the derived type with no template base. Unlike CRTP, it follows the static type at each call.
solid answer
~40 sC++23 lets a member function declare its object parameter explicitly: `template <class Self> auto scaled(this const Self& self, double k)`. When you call `v.scaled(2)` on a `Vec2`, `Self` is deduced as `Vec2`, so a plain, non-template base `VecOps` can return and manipulate the derived type with no `static_cast` and no `VecOps<Vec2>` argument to get wrong. Differences from CRTP: `Self` is deduced from the static type of the object expression at each call, so calling through a `VecOps&` gives `Self = VecOps` and the derived members are not found; a further-derived class gets its own type rather than the one named in a base list; such a function cannot be `virtual`, and `this` cannot be used inside it. It needs a C++23 compiler, and each such deduced-`Self` member is a template.
code
cpp · 23 lines#include <iostream>
struct VecOps { // not a template: one base for every vector type
template <class Self>
auto scaled(this const Self& self, double k) {
Self r = self; // Self is deduced as the derived type, no cast
r *= k;
return r;
}
};
struct Vec2 : VecOps {
double x = 0, y = 0;
Vec2(double x_, double y_) : x(x_), y(y_) {}
Vec2& operator*=(double k) { x *= k; y *= k; return *this; }
};
int main() {
Vec2 v{2, 4};
Vec2 h = v.scaled(0.5);
std::cout << h.x << ' ' << h.y << '\n'; // 1 2
// const VecOps& base = v; base.scaled(2.0); // Self = VecOps: no operator*=
}go deeper
Recall the syntax: the object becomes the first parameter, written this Self&& self, and Self is deduced at the call.
Rewrite a CRTP base as a plain base with explicit-object members and explain why no static_cast or base-list argument is needed.
State where behaviour differs: static type at the call, further-derived types, no virtual, no this, and per-type statics that only CRTP gives.
Set the codebase rule by its minimum standard: CRTP below C++23, deducing this for function-only bases above it, CRTP kept where per-type base classes matter.
## The C++23 feature C++23 added the **explicit object parameter**, popularly called **deducing this**. A non-static member function may declare its object as its first parameter, prefixed with `this`: ```cpp struct VecOps { template <class Self> auto scaled(this const Self& self, double k) { Self r = self; r *= k; return r; } }; struct Vec2 : VecOps { /* x, y, operator*= */ }; ``` When `v.scaled(0.5)` is called on a `Vec2`, the object expression `v` is the argument for `self`, and ordinary template argument deduction makes `Self` equal to `Vec2`. The base therefore learns the derived type **from the call**, not from a template argument written in the base list. The feature-test macro is `__cpp_explicit_this_parameter` (`202110L`). ## How it replaces CRTP | | CRTP | Explicit object parameter (C++23) | |---|---|---| | Base | class template `VecOps<Derived>` | ordinary class `VecOps` | | Derived type comes from | the argument in the base list | deduction at each call | | Reaching the derived object | `static_cast<Derived&>(*this)` | `self` already has type `Self` | | Wrong-argument slip (`Feet : VecOps<Meters>`) | compiles, undefined behaviour | impossible: no argument to write | | Value category | separate `&`, `const&`, `&&` overloads | one function: `this Self&& self` deduces it | | Standard | any | C++23 | Two practical wins follow. The common base is a real, single class again, and the `const`/non-`const`/rvalue overload sets that CRTP bases and accessors often duplicate collapse into one function template taking `this Self&& self` and using `std::forward<Self>(self)`. The overload collapse looks like this. One accessor serves lvalues, `const` lvalues and rvalues, and returns the matching reference type in each case: ```cpp struct Buffer { std::vector<std::byte> bytes; template <class Self> auto&& data(this Self&& self) { return std::forward<Self>(self).bytes; } }; ``` Called on a `Buffer` it returns `std::vector<std::byte>&`, on a `const Buffer&` it returns `const std::vector<std::byte>&`, and on a temporary `Buffer{}` it returns `std::vector<std::byte>&&`, where the classic version needed three or four overloads. ## Where it behaves differently 1. **Deduction uses the static type at the call.** Calling through a base reference, `const VecOps& b = v; b.scaled(2.0);`, deduces `Self = VecOps`. The body then looks for `VecOps::operator*=`, and Apple clang 21 reports `no viable overloaded '*='`, noting the instantiation `VecOps::scaled<VecOps>`. It is still static polymorphism; it does not dispatch on the dynamic type. 2. **A further-derived class gets its own type.** With `struct Vec2Named : Vec2 {}`, CRTP's `scaled` still returns `Vec2`, the type named in the base list, while the explicit-object version returns `Vec2Named`, because that is what the call deduces. Usually that is what you wanted; occasionally it surprises code that expected the base list's type. 3. **It can never be virtual or static.** The standard forbids a member with an explicit object parameter from being declared `static` or `virtual`, or from carrying `const` or `&` qualifiers, since the qualifiers move onto the parameter's type. Clang reports `an explicit object parameter cannot appear in a virtual function`. 4. **No `this` inside.** The body names the object only as `self`; members are reached as `self.x`, not as `x` or `this->x`. 5. **Access still applies.** If `Vec2::operator*=` is private, the base's template cannot call it, exactly as with CRTP, and needs a friend declaration. ## What it does not replace CRTP also makes the **base class distinct per derived type**. The per-type static counter mixin depends on `InstanceCounter<Mesh>` and `InstanceCounter<Texture>` being different classes; a single non-template base with deducing-this members has one static member shared by all. So deducing this replaces CRTP where the base needs the derived *type in its functions*, not where it needs a separate base *class* per derived type. ## Choosing between them - The codebase builds as C++17 or C++20: CRTP is the only option. - C++23 is available and the base only provides functions: prefer the explicit object parameter, because the wrong-argument slip cannot happen and the base list stays readable. - Per-type static state or per-type base types are required: keep CRTP.
- Can an explicit-object member function be virtual so that a call through a base reference dispatches?No. The standard forbids declaring a member with an explicit object parameter `virtual` or `static`. The deduced `Self` comes from the static type of the object expression, so a call through `VecOps&` instantiates the function with `Self = VecOps`. If you need dispatch on the dynamic type, that is still a virtual function in an ordinary member.
- Can deducing this replace a CRTP mixin that keeps a static counter per derived type?Not directly. That mixin works because `InstanceCounter<Mesh>` and `InstanceCounter<Texture>` are distinct classes with distinct static members. A single non-template base with explicit-object members has one set of statics shared by all derived classes. Keep CRTP when the base must be a different class for each derived type.
- Self is deduced from the object's dynamic type, so the call dispatches like virtual.
- An explicit-object member function can be marked virtual to override a base function.
- Deducing this works in C++20 compilers as an extension everywhere.
- Inside an explicit-object member function you can still use this.
- Deducing this gives every derived class its own static members, like CRTP.
Assumes C++23, which added explicit object parameters (deducing this); C++17 and C++20 code must use CRTP instead.