Hello, and welcome to C++ Weekly. I'm your host, Jason Turner. I'm available for code
reviews and on-site training. In this episode I want to discuss
a little bit of some announcements about the channel and things that I am looking forward to
in 2022 as well. As you know, maybe a product announcement that you might already be aware of,
but I think some things that will be interesting to pretty much anyone who has any interest in C++.
So the title of this episode is "Making C++ Fun and Accessible." I find that it's very easy for
us to get caught up in a lot of the minutia and esoteric nature of this language, and we like
to focus on the details, and really we kind of miss the forest for the trees when we're talking
about C++. I do think that there is a fun, simple, accessible language here that we can all enjoy if
we're willing to spend some time having fun with the language, and so I've been doing some work to
focus on that for 2022. Now the first thing that you may already be aware of is that I announced
this Object Lifetime Puzzlers Book 1. Now the fact that I called it Book 1 should be a clue to you
that I am intending to create more than one of these books. So this is Object Lifetime Puzzlers
Book 1. It's available on Leanpub, suggested price is $5.99. The minimum price is $4.99.
Why $4.99? Because that's the cheapest that I can sell a book for on Leanpub. I want to make this as
accessible as possible, but I do have to consider that I need to make a living. It is also available
on Amazon, and I was for a moment reluctant to show you that it currently has a one star rating,
which I am sure that you will notice. Let's just go ahead and click on this to be clear
that this person gave it a one star rating because it wasn't printed in color.
It says that it's advertised as having color but the copy they received doesn't have color.
Unfortunately Amazon has made a mistake here. If you click on "Look inside," you will see that it
does actually show the code as being in color, but that is not the case. This is a black and white
printing of this book. If I were to have offered a color version of it it would cost several times
this price, and at $9.99 I would be making about, I think - $5 dollars per book sale. So I tried to
make it as affordable as I could and sell it on Amazon here, so I'm going to have to work with
Amazon to make sure that they properly show that this is a black and white printing of this book.
Available on Amazon, should be available in most markets, it's $9.99, Prime shipping, that kind of
thing. You should be able to get it pretty easily. I'm presenting to you this new puzzle book,
and I know that a lot of people (because I've asked around on Twitter, and with my patrons,
and on Discord, and that kind of thing) a lot of people expect that if you say C++ and puzzle in
the same phrase that it's going to be pain, and the goal, like I've said already at the top of
this episode, is "fun and accessible." The goal is not pain. The goal is that you should have fun
doing these puzzles. I decided that I'm going to go through a couple of examples here so you can
see what the puzzles look like, so I've got my printout in front of me which I've printed from
the pdf version that you could get if you buy it on Leanpub and you choose to print your own
copy locally, or it would be a black and white one here, but I really don't think color or
black and white has any impact here. The idea, the whole concept of these (and sorry for the
shadows and lighting - I'm not perfectly well set up for this kind of recording) is that an object
"Says its first name when its lifetime begins, and it says its last name when its lifetime ends." So,
if we're going to go through these objects which are two 'automatic' ("Automatic") objects (I do
describe this in the book) their lifetimes begin as soon as they are seen, and their lifetimes end
when they go out of scope. You get scope when you see one of these closed braces - that's the
end of the current scope. Lifetime begins in the order that objects are seen and
ends in reverse order, so I get my first object - object one. Its lifetime begins,
and I get a "p" printed. I'm gonna cross out that 'p' - this is how I like to do it personally,
and then object two lifetime begins and we get an "L" printed. Then we see this close brace,
and that means that automatic objects that were declared in the scope are going to have their
lifetimes end in the reverse order that their lifetimes began. So we've got "PL US" like
this. So we read backward - "P L U S," and you know, that one's pretty
straightforward. It gets a little bit more exciting when we get to things that
actually have more scopes, so we're going to see object one - its lifetime begins with an "S,"
and then (I'm going to cross that out) and then we're entering this scope here,
we're entering a new scope, and I see a new object. Object two has its lifetime began
that's a "T." Now I've got a closed scope here, so I need to see what objects were declared inside
of these set of braces and have their lifetimes end in reverse order. So I'm going to get an
"R," and then I'm going to cross this out and say I have now dealt with the end of this scope.
Then I have object three have its lifetime begin, and I'm going to get a "T" printed, and then I've
got this next closed scope. So I have to deal with all of the objects that were declared
here inside of the scope in between these two sets of braces;
and I'm going to get, in opposite order of their lifetimes begin - "O" and then "L."
So I got "strtol." Now this should be a clue to you that all of these puzzles are actually
things that are related to C++ development in some way, so if you see this "strtol"
and you have absolutely no idea what that means, then you can just search for it real quick;
and we can see that this is a function for taking any byte string pointed to by a string. It takes a
string of characters and returns an integer value to you. So, okay, so maybe you just learned about
a new function in C++. That's pretty cool. So then after 'automatic' lifetime I introduce 'static'
lifetime, and with 'static' lifetime an object's lifetime does not end until the program is done
running. So sometime here, not when this brace is closed, but sometime down here when the
program is done running I'm going to get my 'static' lifetime cleaned up. So the braces
are kind of just there to confuse you with static objects, but this is exactly how it works in C++.
All of this is real C++ code, and I'm sure that if you have interest in C++ or not that you can
find these fun. I already know that my niece has had a blast going through these puzzles
and she has no interest in C++ whatsoever but had a lot of fun doing this; and now, interestingly,
uh, she knows more about object lifetime in C++ than many C++ professional developers that I know.
She might not know that she knows this, but she does. We're going to go through these, and in the
order of declaration I've got "U N" (I'm going to cross these off); "E," cross that off; and now I
have to deal with this. The only object declared in here was 'static' so there's nothing to do;
and then "X", cross that off; and then another closed scope but only objects that are 'static'
are in here, so there's nothing to do with it. Then we get "P," and then another closed scope.
Again, only 'static' objects so nothing to do, but now we have to go back and clean up the
'static' objects in the reverse order that their lifetimes began. So we're going to get "E C T
E" and "D." "unexpected." So "unexpected," uh, you know, that's has some role in the C++ standard.
When it really starts to get fun, and again my play tester, my niece, agreed here, is when these
things start to get mixed up with each other. So moving on to puzzle 21 (and I should have said
ahead of time that there are spoilers here if you want to get a feel for what it's like - I'm only
solving three out of the 128 puzzles in the book). So I've got my first object - object one. I see
it. Its lifetime begins. I have an "O," and then object two, its lifetime begins -I have a "V."
Then object three, its lifetime begins - I have an "E." Then object four
is an "R." Now I've got this close scope here that I have to deal with,
and are there any non-static objects inside the scope? Indeed, there is. There is object four,
so its lifetime ends, and I've got this "F" printed. Then object five is an "L."
Object six is seen and we have "O," and then object seven has its lifetime begin and we've
got a "W." Now we have this closed scope and we have to deal with all of the 'automatic' lifetime
objects - all the ones that aren't 'static' here. So we've got this closed scope that I have to deal
with that is an "underscore" for object seven, and then object five has its lifetime end - that's an
"E." Then object two has its lifetime end - that's an "R." Now we have to go through and
deal with the 'static' objects that just had their lifetimes end, and they are going to be, again,
in the reverse order that they were declared. So we've got an "R," and then we've got an "O,"
and we've got an "R." (I just said reverse order in which they are declared,
that's not right - the reverse order in which their lifetimes began.) In these simpler puzzles
those are going to be equivalent, but we will have future books with much more complicated puzzles
for those of you who are interested in this. So that is an overflow error. So I hope you see
that these are not puzzles designed to cause pain, these are puzzles that should be fun for everyone.
Now I am pretty convinced that by the time you go through a couple of these puzzle books,
whether you are a C++ developer or not, you will have learned something.
If you are not a C++ developer you're gonna learn how to read C++ code,
which I think is interesting. I think this is a great first step for getting people introduced
to the language. So if you just had fun doing a puzzle book and now you can read C++ code,
actually learning how to write C++ code is going to be considerably easier in the future,
which brings me to the next thing that I want to talk about in this episode. That is this
live stream that I just did - at the time of this recording three days ago - on January 4th, 2022.
"C++ For The Beginner and Non-Programmer - Lesson 1." Now lesson one again implies that
I am planning to do multiple lessons. This is completely an experiment, although people seem
to be liking it. This is a pretty good thumb up ratio for me - 331 for 6500 views. That's
pretty good, and you can just trust me there's not very many dislikes. It's something like 98
thumbs up, something like that right now. I'm trying to figure out how to introduce modern,
good, clean C++ techniques and core C++ concepts to new programmers of the language, and hopefully
solidify the things that regular programmers in C++ need to know. So this is lesson one.
Lesson two will probably be before this episode goes live. I just want to talk about creating
functions in lesson two, where in lesson one I really just got you into a slightly complicated
"hello world" with some basic control flow in it. Now I'd really love to get everyone's feedback
in this process of trying to make C++ plus more fun and accessible
to people who don't know C++, and maybe perhaps to people who aren't programmers at all,
to see if you are having fun with these puzzle books, for example. Now I do have more puzzle
books planned. The next puzzle book is going to be about understanding the differences between
copies and references because C++ is relatively unique in the fact that by default we copy values.
Most other languages these days by default have a reference to another value, so I wanted to make
another book that I hope is fun that will help you to understand copy versus reference semantics in
C++. Again, it's going to be high level. It should be fun for everyone. Please leave comments. Share
this information with other people and give me some feedback if you think that there's particular
things that other new C++ developers struggle with that I should address, or things that you
struggled with when you were learning C++ that you think that I should address in these puzzle books
or episodes coming up. I really want this to be the theme for 2022 for this channel - making C++
fun and accessible for everyone. So thanks for watching this episode of C++ Weekly.