Hello and welcome to C++ Weekly. I'm your host, Jason Turner. I'm available for code reviews and
on-site training. Uh, in this episode I'm going to discuss changes coming in C23 that affect C++
programmers. I'd like to go ahead and take the opportunity to remind you
that you are largely in control of what we talk about on this channel and my [Fill in the
Blank] Programmer channel, and that is driven from my lefticus/cpp_weekly/issues tracker
on Github. You can submit episode ideas, you can thumbs up (or if you really want to thumbs down)
episode ideas that you would like to see or, I guess not see, on the channel; and that brings us
to today. We're on the C23 changes that affect C++ programmers. I would also just like to remind you
that I have published a variety of puzzle books and a Best Practices book for C++,
and you can check the video description below for links for ebook and print book versions. Okay,
so we're going to talk about these C23 changes that affect C++ programmers. Now by effect
I'm largely talking about interoperability between the two languages, but I'm gonna
start with a big hitting one. I'm not gonna go into a ton of detail about it because I have
another episode planned that's just about this one particular feature coming in C23 and that is
#embed. Now the compiler that I currently have configured on Compiler Explorer here
doesn't give me the ability to actually use this particular feature, but you basically can do
something like this and it is going to literally embed that text right here inside your file
and this is going to be used more for creating byte arrays and strings of data that you
then want to process at compile time in some way. It will be used for embedded scripting,
and it will be used for all kinds of embedded resources. Now I know that there are two
camps of people. There is the camp that says, "There's no reason we need this because there
are other ways to do it," and there is the camp of people who says, "This is exactly what I've been
waiting for for the last 30 years since I started programming in C!" I'm more in the second camp
because I know the failures of trying to do this in other ways that involve, like,
doing a #include of a file that happens to have a character string in it or something like that. It
doesn't work like you want it to. #embed will get us there. So why does that affect C++ programmers?
Well if you have a conformant C23 compiler then you will probably have access to this in
C++23, but there's no guarantees there. It's possible that your compiler vendor will say,
"Sorry, that's not technically a C++ feature so I'm going to forbid you from using it,"
but let's hope not. Okay, like I said, I'm going to do a more in-depth episode about
that in the future, so let's move on to the other things. Next on the docket is constexpr. This is
kind of a big deal, uh, but not as big of a deal as we would like for it to be. So C23 is going to
give us constructs for variables but not constexpr functions, so I can't do something like this
because that's a function, but I can do
this. So I can have constexpr values and this is going to help with cross
language compatibility. If I have a header file that I want to be includable from C
and C++ I can do this kind of value in a header file in C23.
The next one is attributes. Now I'm going to go ahead and switch my compiler here. I haven't
actually tested this but we're going to try doing this in actual C mode in C23. Okay, so our
compiler does not yet support this, so we're gonna go ahead and just add some comments and we'll see
along the way what our Clang trunk actually does support. So number three is attributes,
and again this really helps in the case that we want to have a header file that can be included
from our C compiler and our C++ compiler. I personally think this is going to be very
helpful when we talk about nodiscard. Now this is a great one for telling the compiler that throwing
away this value should result in a warning, or if you have warnings as errors enabled,
then an actual error; so we're going to get a handful of attributes from C++
currently in C23. Now I'm focusing on the things that are going to affect C++ developers and I
see that as mostly header files again, but just so you know you will also get
the deprecated, fallthrough, maybe_unused and noreturn attributes, and if you are a fan of
this channel then you are already familiar with at least fallthrough, maybe_unused and nodiscard.
I don't believe I've ever covered noreturn and deprecated but if you would like to see episodes
about that then be sure to go on my Github and add issues for that so that they become
ideas that are tracked for future episodes. Okay, number four -
unnamed parameters in function definitions. Now sometimes you need to meet a certain interface
and you just don't have a reason to name a parameter. Now in modern C++
there's two different ways of dealing with this.
Now we might have something like this, but if we have warnings enabled
then we should get an "unused parameter" warning, which we are getting here. So in modern C++
we might mark this as maybe_unused, which is now going to be allowed in C23 since we have
attributes, or we might choose to simply not give this parameter a name,
and that will also be allowed in C23. So, again, helps with interoperability between the languages.
Now number five is typed enumerations. Uh, since C++11 we have been allowed to specify the
underlying type for an enumeration and it would look something like this. It kind of looks like
you're actually inheriting from an integer here, and you can give these things values. Now before
C23 this wasn't possible, but with C23 you can specify the underlying representation
of an enumeration. Now this is actually kind of important, largely for interoperability between
C and C++, but also because technically the two values here in my enum are 0 and
1, which means that the standard (at least the C++ standard and I believe this is correct for the C
standard as well) requires that the underlying type of enum, MyEnum here,
be at least one bit. It makes no other requirements on it, so if we were to specify
that this is, in fact, an integer, or perhaps it has an underlying type of char, then we can
specify the actual size here. That's pretty cool. Now the final thing that I want to mention is the
_has_include. So now again, in this shared world of C and C++ header files, we can do...
we can do this. We can make a compile time a query to see if a file can be included,
and then we can do something with that knowledge once we know if that file can be found on the file
system currently. So all these together really add up to much better interoperability for those
shared header files between C and C++ with number one, #embed, being kind of the killer feature that
hopefully we will have access to in C++23 when our compilers become conformant with the C23
standard. So thank you for watching this episode of C++ Weekly. Be sure to subscribe,
give this video a thumbs up and check out all the links in the video description.