Welcome to C++ Weekly.
I'm your host, Jason Turner.
I'm available for code reviews, contracting,
and on-site training.
Now, first of all, this is the first time
that I'm using a new microphone set up.
And I hope that everything works out well with this.
We'll see how it goes.
But this is an episode on C++11.
And this episode is continuing where I left off on the episode
on C++98, which was basically explaining what really makes
c++ unique, and what features of C++98 you should be aware of.
This episode is continuing with C++11,
and I will do future episodes on 14, 17,
and probably 20 someday.
But I don't know exactly when that will be.
It will be dependent on when C++20 is finalized.
And when we have compilers that support all of said features.
So I am in the Compiler Explore, as usual, here.
And I've got C++11 set up as my standard.
I am on Clang Trunk at the moment.
But really, I am mostly using this just as a tool
to show you what features I would like to discuss.
So the main thing that C++11 added, in my opinion, is auto.
Now, there's some contention exactly
about how often you should use auto.
And there used to be the almost always auto standpoint.
And I don't know how many people say that today,
but I will say--
and this is the way that I like to put it--
is use auto when you don't care what the type is.
So for example, let's say I want to count some number
of elements in a vector.
And I've got the count algorithm.
And I want to do something with this count here.
Now, the question that you might ask
is, what is the return value type from the standard count
algorithm?
And I would posit that a perfectly reasonable answer
is, I don't care.
So I can do this.
With C++11, I can just say, I don't care what the type
of this thing is.
Do whatever is reasonable.
And this actually makes things with generic programming
much, much easier than they used to be.
If I've got anything that can be used with the count algorithm,
then I can just use auto here now.
But in the real world, auto is almost invaluable.
So this was the first item that I wanted to mention.
Now, the second one is ranged for loops.
And ranged for loops really, particularly
shine with auto, as well.
So let's take this count things vector that we have here,
and let's just say, I don't know,
we want to do things with something.
And that's kind of the point here.
We're in generic programming land.
And this is particularly helpful.
Now, with a pre-C++11 ranged for loop we would end up with--
well, let's take this out from being a template
so that we can really bring this home.
If we wanted to iterate over the elements of this vector,
we might do something like this.
This is in C++98.
This becomes very complicated if we
wanted to use a template here.
So we're going to leave this just as this vector of int.
We know the type here.
And we will take this into a C++11 for a ranged for loop.
The amount of boilerplate code that is removed here,
is absolutely astounding.
And using auto here is particularly helpful.
And using a const auto reference is even more helpful
because it can eliminate the possibility of things
like slicing.
Getting this right in C++98 was rather hard.
And if we want to bring this back into template land now,
we can in fact iterate over any container
of any things with a very simply written ranged for loop.
The third important thing that C++11 added is lambdas.
I already mentioned this count algorithm here,
but let's just say we want to count all of the things that
are less than three.
Like this.
And a lambda in C++90--
well, a lambda in C++11, that is,
allows us to do something very simple like this.
We can say, I want to take an int, and that is this.
And then I want to do return i is less than three.
And our lambda declaration here is quite handy.
This has no captures at the moment.
And I have made many, many C++ Weekly episodes about lambdas.
So if you want to know anything about that,
then I suggest you go to one of those other episodes.
And this is the parameter list.
And then this is the body of the lambda.
It's a very simple way of creating a function object.
And it saves us a lot of boilerplate code to get.
The fourth thing that I would like to mention that C++11 gave
us is variadic templates.
Now, I'm not going to spend a ton of time on this.
Again, there's been about three or four episodes of C++ Weekly
just on variadic templates.
But I will say just for your--
to get your interest here, that it looks something like this.
Now, inside of here, I can expand the set of parameters.
And in C++11, this is a little bit limited,
but let's do something like this.
This is the most simplistic way of expanding a variadic
parameter set in C++11, and using this as a way
to generically call some sort of a function.
Now, I did a much earlier episode of C++ Weekly
on a variadic template refactor of ChaiScript,
which you might find interesting in this context here,
to understand where variadic templates can really help
things out.
The fifth thing that I would like to mention is unique_ptr.
This is a pretty handy thing.
It fixes some bugs in the language with auto_ptr.
Auto_pointer never really should have existed.
It never did quite what we wanted it to do.
But that's OK because C++11 gave us move semantics,
and move only types, and unique_ptr.
And we end up with something like this.
We can, in fact, create a function.
So I have just allocated a pointer on the heap and C++11.
And I know when this goes out of scope, that this value is going
to be popped from the stack and its memory
is going to be freed.
And this goes back to destructors in RAII and those
very important things that C++98 set the framework for.
So go back and watch the episode on C++98 and C++03 for some
more background on that.
Now, the final thing that I would like to mention
is constexpr.
Constexpr in C++11 was indeed quite limited.
But you could create a constexpr function that calculates
some value of some sort.
And it can only call other constexpr functions.
And it can only have a single return statement.
And it's basically very limited, but you have to--
in C++11, anything that's constexpr has to be expressed
in a recursive fashion, basically.
But you can do pretty much anything.
The limitations that we had in C++11 are quite arbitrary.
Those get cleaned up in later versions of the language.
And I will discuss those later when I talk about C++14
and C++17.
But I can do something like this.
So in C++11, I can calculate a value and I can get access
to it at compile time.
And you might wonder why in the world constexpr here.
This means that this value--
the value actually called value--
is available in a compile time context.
So that is important for if I wanted
to create an array of some size of these things.
And I can in C++11 using constexpr like this.
So I've got an array of whatever the value of this is,
which is an integer that is 5 times 13.
So the size of this array is 15.
So there you have it.
This is what I would say is the six most important things that
C++11 added to the language.
And these are things that you need
to know something about to really get
the most out of the language.
Of course, there are many other things.
C++11 was an absolutely huge update to the language,
and one of the most significant things that happened in the c++
world.
So I'm definitely simplifying things here,
but check these out.
And I hope you enjoyed this episode of C++ Weekly,
and that you got something out of this.
Be sure to subscribe.