Hello and welcome to C++ Weekly. I'm your host, Jason Turner. I'm available for code reviews
and on-site training. Now in this episode we have a couple of new pre-processor things that we need
to discuss that came in in C++23 and will probably be available to us in C++. I've hinted at one of
them, well, directly mentioned one of them before. The other one is not one that I've mentioned yet.
[Music] Now as you can see here, the first one that I'm going to discuss is hash (or #) embed,
depending on where you come from, and I have up here a link to JeanHeyd Meneide's
blog on #embed. Now this has been something that JeanHeyd has been working on for five
years. This has been a long time coming, and for those of us who have done things
with embedded scripting or large embedded things of any sort, I mean, embedding resources in your
C or C++ projects tends to be rather painful. #embed is going to solve that problem by giving
us a clean cross-platform way of doing that. I'm going to demonstrate this in a very simple way,
but before I do that I do want to remind you to check out the description of this video and make
sure you provide any episode ideas that you think could be interesting. I am slowly working my way
through this - as you can see I currently have 178 open ideas so it is very important that you
come in and actually vote on the things that you find interesting. I will not be going in a strict
order here because I can't necessarily - some things take a lot of research and some things
simply don't have compiler support yet, and I do wait until there is some form of compiler
support before I actually demo a feature. Also, of course, check out links to my books, Patreon,
t-shirts, you know, all the stuff that helps support this channel. So I have gotten a fair
number of comments lately about the size of my fonts and stuff. This example does have a fair
bit going on - I will try to make this all as readable as possible. This is a CMake based
project in Compiler Explorer. If you don't know what Compiler Explorer is then I suggest you watch
some more of the episodes of this channel or watch some of Matt Godbolt's talks on Compiler Explorer,
but this is compiler-explorer.com. It supports CMake projects. This is a relatively advanced
feature of Compiler Explorer that does not get used very often, but this is a pretty simple
project, it just has one file at the moment. It's named file2 - that's because, well, I reused an
earlier project, and we've got an input file and this, as well, (is) a whole lot of text.
In this code I have a 'static constexpr char ' array and I open it up and this is where I'm going
to put the data, and I have a preprocessor macro, and all this does is it takes this file and just
smashes it in here. So if you can see down here I have "wow a" printed. Let's make this bigger.
So it says "wow a" down at the bottom.
This is because I have used the feature of limiting how many bytes can actually be read,
and this feature exists partially because you might do something
like this, which I think would be completely valid, although yeah,
I'm not sure what Compiler Explorer is going to do here. Oh. Intriguing.
So this exists currently as a compile time literal. It is reading five bytes
from dev/random - fascinating thing to do. Let's see if we can actually print this.
Okay, yes, so I have a read five random bytes from random, and they were embedded into this
executable at compile time. We can see that it's just a single call to 'puts' after setting up some
stuff on the stack. Let's go ahead and put this back to our input.txt, and now if I was actually
trying to read from dev/random then that could have caused a problem because it would have read
an infinite number of bytes if I had not put a limit on there. So I have this input.txt and if
I remove the limit then I should see, well, a whole lot of text printed down here when it's
done compiling and executing, and there you go. So this is designed to ease the pains with embedding
resources into your C++ projects. Now I know a lot of you are going to say that's useless and
a lot of you are going to say I've been waiting for this my entire life, so go ahead and leave
a comment on which side of the fence you are on there. Those of you who think that it's useless,
read the comments from people who say they've been waiting for this for a long time and perhaps
you'll get a better idea as to why it is not useless. Okay, so this brings us to the second
thing that I want to demo, and again, this is not very complicated feature but it's the fact
that we can actually give a warning directive in C++23 and in C23, and that's kind of cool.
So you might do something like this and check to see if something is just outside of what you
have been able to test on your project, for example, or warn if there's some non-ideal
compile time situation, which I'm guessing any of us who have written cross-platform code in
the past has dealt with this kind of scenario. So it's pretty straightforward - it's a new way
to actually generate a compile time warning, and you can see it actually gets generated
here. So that's really all there is to say about this particular feature, just a standard way of
doing this now across all compilers. So thank you for watching this episode of C++ Weekly.
Be sure to subscribe. Hope you enjoyed this episode, and make sure to leave a comment.
[Music]