Home / Forums / Understanding Deducing This (Explicit Object Parameters) in C++23 [Part 3]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Tutorial

Understanding Deducing This (Explicit Object Parameters) in C++23 [Part 3]

FlatMapFan
Cache-Friendly Containers
MEMBER
Rep: 353
Join Date: Oct 2023
Posts: 6
Thanks: 97
2y ago · Oct 1, 2023 2:15 PM
#1
C++23 introduces Explicit Object Parameters (colloquially "Deducing This"). It allows passing the this pointer as the first explicit parameter in member functions, drastically simplifying const/non-const and lvalue/rvalue method overloads.
FlatMapFan · Cache-Friendly Containers
Sorted vector flat_map vs red-black tree std::map benchmarks in h...
The following users thanked FlatMapFan for this post:
ConstexprKing
C++ Metaprogramming
VIP
Rep: 219
Join Date: Jan 2020
Posts: 6
Thanks: 108
2y ago · Oct 1, 2023 3:57 PM
#2
Instead of writing 4 separate overloads for operator[] (const, non-const, &, &&), you can write a single template method: template <typename Self> auto&& operator[](this Self&& self, size_t idx) { return std::forward<Self>(self).data[idx]; }!
ConstexprKing · C++ Metaprogramming
Compile-time evaluation, type traits, and constexpr DSL engines i...
LockFreeLarry
Concurrent Systems
MEMBER
Rep: 106
Join Date: Sep 2022
Posts: 6
Thanks: 45
2y ago · Oct 2, 2023 7:57 PM
#3
It also makes implementing the Curiously Recurring Template Pattern (CRTP) trivial without inheriting from template base classes.
LockFreeLarry · Concurrent Systems
Lock-free SPSC/MPMC queues, memory orders, atomic fences, and RCU...