Hello and welcome to C++ Weekly. I'm your host, Jason Turner,
available for code reviews and on-site training. You should contact me about
getting some on-site training for your organization. It's a great opportunity for
team building. In this episode I'm going to cover the important parts of C++20.
[Music]
Make sure you check out that video description for ways that you can support the channel and
upcoming events that I might be holding in your area. Now these are going to be in no particular
order, but I thought it was time for me to follow up on the important parts of 98, 11,
14 and 17 that I've already recorded with the important parts of C++20.
Okay, so this is designated_initializers in C++20. We can specify the values that
we want for each of the elements, and it is worth noting that I have done episodes on (I
believe) every topic that I'm going to cover except for one. Now note that I cannot leave
a value out and I cannot initialize things out of order. I also cannot leave one off the end.
I would love some sort of a tag or something that makes me say that this particular
object is required to be initialized with designated_initializers, but that's not an option.
If I want to compare these things by less than comparison, pre-C++20 I would have to
implement my own less than comparison operator. As of C++20 I can do this:
Now to get this to work I do have to include the header but then the compiler does the
rest of the work for me. This can do all of the normal comparisons, and they're all built in right
there for me in the standard. Now Modules are very important, but compiler support, tooling
support is still very much in flux at the time of recording of this episode, which is May 29th 2023.
So I can't really give you a succinct example of this,
but the idea is that Modules will make it much easier to, well, modularize our code.
Keeping in the topic of things that I cannot give you succinct examples about are also co-routines.
Look into the cppcoro helper library for this - there's no standard library support,
it takes lots of boilerplate code, but you do need to know that that is one of the main
defining features of C++20. Now I have talked a fair bit about Concepts off and on over the years,
and you can check out any of the episodes on that particular topic,
but we can use Concepts to constrain the types of parameters that can be passed to
our functions. That is awesome. It can be used in many different ways. I'm not going to go into all
of the ways here, but I do want to make an aside that one of the most interesting things to me
is that we can have just generic 'auto' parameters,
and this has implicitly created a template for us. The header is incredibly useful,
but this is now part of C++20. This is the general idea; it's very much like Python
string formatting. The header in C++20 gives us compile-time
information about the particular line of code that we happen to be executing,
so we have now created a string that would be equal to event 42 : do_log, so that's pretty cool.
Now here's a topic that I have not touched on whatsoever, and that is calendar updates
to Chrono. I am not yet really going to dig into this, but I do just want you to know as
of C++20 you can do all of the things that you want to do with calendars, and if you've ever
done anything with dates and times and time zones in any programming language at all you know this
is incredibly difficult to work with, so be sure you check this out. This is becoming more and more
supported by our standard libraries, and we've got lots of options here. Important notes that
it does have support from std::formatter to work with the header, and I believe it should,
in theory, be able to work with multiple different calendars, not just the Gregorian calendar.
Ranges I have already covered somewhat, but they are now number nine on here,
and the most trivial of niceties,
such as being able to simply drop the first two elements in a range without having to worry
about iterating past the end of a container or something like that, or checking to see
whether or not there are still elements in the container - incredibly helpful in everyday code.
I'm not going to get into a lot of details here, but we got a lot more 'constexpr'. All of this
means virtually anything can work at compile time now with just a little bit of extra effort.
is probably also been very important to me when working with C++20,
and absolutely worth mentioning here that I can just do this, and I can very easily decouple my
use of ranges to anything that happens to work with a contiguous container such as vector,
string_view, string or array. So that's helpful as well, so , keep your eye on that.
Then finally there have been a lot of threading updates, and going through all of them is by far
outside of the scope of this particular episode, but if you do lots of multi-threaded programming
you should check out jthread, which I've done an episode on, as well as the counting_semaphore,
binary_semaphore, latches and barriers, all in C++20. So thank you for watching this episode of
C++ Weekly. I hope that this was a beneficial, good episode; and be sure to subscribe.
[Music]