Hello, and welcome to C++ Weekly. I'm your host, Jason Turner.
In this episode I'm going to do a high level overview of Deducing 'this'
from C++23. We'll talk about the use case and some of the odds and ends that it allows.
So if you've ever
had to implement both const and non-const versions of a member function, then you'll have some idea
why this new utility from C++23 was added. So let's say I have a templated matrix type.
Now to store this data I'm going to use a std::array
and I want to be able to provide some handy accessories for it.
but that's relatively simple - I can return reference to this thing.
(Now you might notice that I am actually using Visual Studio here, and that's because this is
the only compiler that supports Deducing 'this' today.) So I can see that 15 is returned from
main, and that's exactly what I expected to see returned from main, but if we actually want this
type to be usable now I need to provide a const version of the overload. This is a very normal
thing to do. All of our container types have const and non-const accessors like I just provided here,
but if you've read my C++ Best Practices book you may have noticed that I just broke one of the
first rules that I have in there, and that is to not copy and paste code. So
that's bad. I just now have to deal with two different implementations
of this, so one option that I have been a proponent of in the past
is to provide a version like this. So I've got a static templated private helper function
that I can dispatch to so that I don't have to
repeat myself multiple times here. Deducing 'this' exists primarily to alleviate this problem,
and the result ends up looking something like this.
You can see that we can use it in const and non-const cases, but we're taking a
forwarding reference to ourselves and an x and a y; and I'm just able to call this with ".at(2,
3)" and this does exactly what I would expect it to do. It works in every case. It's a templated
function though and that's going to have some upsides and downsides, but it automatically
deduces what you needed to deduce and you don't have to write any of this boilerplate code.
Now if you are like me you might notice that I can't access data directly in here -
"illegal reference to non-static member," so this thing, this Deduced 'this' member function isn't
a static member, but it looks and smells an awful lot like a static member.
If I try to access member data then I'm going to get a "can't access non-static data" error,
but you're not in a static function you would say. There's no static keyword here so
what's the deal here? And if I were to try to directly access this thing
then I'm going to get this "no match to overloaded function...I expected
three arguments but four were provided," which,
well, that doesn't really make any sense at all because I passed three, and it wanted three. So
that error might need a little bit of help, but I can use free() function syntax here to
actually get a pointer to this thing and treat it like a static member function if I wanted to.
All right. Well it took a little while to get there. I'm able to get a function pointer to this
that is the at() function, and now I can call this thing as if it were a free() function.
So yeah, there you go. This is Deducing 'this' in a nutshell. There's a whole lot
more to say about this particular feature but I'm going to leave this here for the moment. We will
definitely come back to it in future episodes, so thanks for watching this episode of C++
Weekly. Be sure to subscribe. Give the video a thumbs up.