Hello and welcome to C++ Weekly. I'm your host, Jason Turner. I just got finished
recording this episode on deducing 'this' and I still had this matrix example up, so
I thought it probably made sense to show another important new feature of C++23.
We definitely have a couple of questions about this code. First of all I've called this thing
'at', which 'at', in the parlance of the C++ standard library, means that it's going to throw
an exception if it cannot access the particular piece of data that you are trying to access;
and I have delegated that responsibility to the std::array 'at' function. This has a
minor problem in that I don't know where it went wrong. Arguably this should be something like:
at which point this ".at" here becomes redundant because I've already checked to make sure that
this will fit within the confines of what I know to be true here. So we can have this version,
and I just really wanted to point out that this 'at' is
an exception throwing function, but that's really not the point of the episode at all.
The point of the episode is that C++23 allows for multi-dimensional subscript
operators. Now I'm breaking my rule again of doing a copy and paste, but hopefully it
will make sense in just a moment as to why I thought it seemed like a good idea here.
Now unfortunately Visual Studio doesn't yet support multi-dimensional subscript operator,
but it's the only compiler that does support deducing 'this', so we're in
a little bit of a catch-22. We're gonna have to change compilers and refactor this code slightly.
So really it's just this simple. We are able to use a multi-dimensional subscript
operator finally in C++23, and hopefully at some point we'll have a compiler that supports a full
set of C++23 features and we can really dig in to the possibilities here for how we might implement
a multi-dimensional matrix in C++23. I think that could make a pretty interesting episode.
Just to prove that this is actually returning something useful, I directly initialized the
first 16 elements of our 5x5 matrix with these values, and I can see that I am getting a value 13
back out, which makes sense. If it's an x of 2 and a y of 2, then I should be incrementing, yeah,
to the 13th element. That makes sense taking into account the fact that this thing starts from 0.
So yeah, that's just a very small feature of C++23 - we have a multi-dimensional
subscript operator support. So I hope you liked this episode of C++ Weekly. Be sure to subscribe.