How ref qualifiers led to deducing this
published at 28.05.2026 16:21 by Jens Weller
Save to Instapaper Pocket
Last week I shared an overview on ref qualifiers with you, this is a follow up on this post. Featuring deducing this, a C++23 feature that should be available in your compiler if its been released in 2025 or later.
Lets start with two more things you may want to know about ref qualifiers. First, const is also supported for the rvalue version: m::f()const && exists, though this is mostly not very useful. Thats why you rarely see it covered, as const rvalues are unusual to unvalid. But thats where m::f()const&&=delete comes in, it allows you to turn these object states in to compilation errors.
As ref qualifiers are also often used in shaping conversions from one type to another, Peter Sommerlad points out that there is a hole in the type system in C++. Compiler generated assignment operators are not ref qualified, which enables you to assign a reference from a temporary object: auto& ref = (T{}=T{}); - which then leads to a dangling reference.
During my research on ref qualifiers I've seen mentions of deducing this, but only later realized how these features are connected and actually ref qualifiers inspired deducing this. one of the great features of C++23! Using ref qualifiers allows you to fine tune the behavior of a class, but you may pay the price for this with code duplication. At least the member function now is split into multiple declarations, which may often also have identical code. Also adding to the overload set might not always be the greatest idea.
So deducing this allows you to also inspect the state of the instance that is calling the member function, aka this. Often this is done in an implicit way by using a template or auto parameter for deducing this:
void member_func(this auto&& self)
Which makes self a universal (or forwarding) reference, allowing you to write code that deals with this in a generic way. You can still introduce special handling with if constexpr and using std::is_rvalue_reference when you'd explicitly need to have special rvalue handling. But only do this if your really need to, std::forward exists and will mostly handle things for you. If you want to turn rvalue based calls to a member function into a compilation error, you also can achieve this with deducing this:
void member_function(this auto& self)
So a lot of the features of ref qualifiers are now also covered by deducing this, and in a better form. So if C++23 is available to you, you may prefer this newer feature. If you need to support older C++ standards, ref qualifiers are still available to you. But deducing this is far more then a better ref qualifier approach, its use cases go beyond what you can do with ref qualifiers. Most of them should be listed in the linked blog post above. For the moment I think that is actually m::f()const&&=delete; that users of deducing this may should have in mind, as this is AFAIK something you can't do with deducing this.
One can also not combine these features: a member function using deducing this can not be qualified with cv (()const) or ref qualifiers.
Join the Meeting C++ patreon community!
This and other posts on Meeting C++ are enabled by my supporters on patreon!