Hello, and welcome to C++ Weekly.
I'm your host, Jason Turner.
I'm available for code reviews, contracting,
and on-site training.
In this episode, I am going to discuss what makes C++ 14
unique, and what you need to be aware of to really get the most
advantage out of the language, and what the main features
of C++ 14 are.
Now, the last episode that I recorded was the episode on C++
11.
This is not exactly how these will air.
I am aware of that.
But I have up here the exact examples that I last left off
on in C++ 11.
And I'm going to use them to demonstrate how C++ 14 changed
the language.
Now, C++ 14, in many ways, in my opinion,
is simply a bug fix to C++ 11.
There's many little things that it adds.
And most of them really come down
to making the language more consistent internally.
Now, starting up here at this count things here, I have auto.
But I can add to this auto return type deduction.
So this function right now, I am currently returning void.
And I already said I don't care what the type of count is.
If you didn't watch the episode on C++ 11,
I suggest that you go and watch it now.
I'm going to go ahead and change my compiler standings to C++ 14
here.
And if I wanted to return this count,
because I don't care what the type is, I can, in C++ 14,
actually say this is full auto return type deduction.
And this will proceed as expected.
Now, our arranged based four loops did not change in C++ 14.
So I'm going to go ahead and remove this example.
But our lambdas, on the other hand,
changed in a couple of different ways.
The first is generic.
So I can, in C++ 14--
right now, I am saying that this is
an integer i being passed in.
But this is something that counts anything
less than three.
Technically, this function could work with longs,
floats, doubles, long longs.
It could work with anything.
So it is handy--
in C++ 14, I can make this an auto parameter type.
And it doesn't matter what I get past it.
I have asked the compiler to implicitly create
a template for me.
So this is item number two now.
And we also get this generalized capture expression thing in C++
14.
And I will try to do my best to demonstrate this simply.
In the C++ 11 example, I did not explicitly say what the capture
can be, or how they are used.
And you can go and look at other videos for that.
I am just going to make a different version
of this function here.
Right now, I've got some value here.
I can, in C++ 14, do this.
This is known as a generalized capture expression.
I can put pretty much anything I want to in here.
And as I've said before, I've made many,
many videos on lambdas in C++.
So I'm not going to go into too much detail here.
But let's just suffice to say I can do something really weird
like this in C++ 14, and this is completely valid C++.
I have, inside of my capture expression,
declared a new lambda that returns the value 3,
immediately invoked it, and use that to capture this value
here, which is the value 3.
Now, this value is automatically deduced.
There is no explicit way for me to say,
I want this to be a float.
That's not possible.
That won't compile in any version of the language.
This is going to be whatever the return type of this is.
So if I really wanted this to be a double,
I could do something like this.
I could do something like this if I want it to be a float.
Or I could do something like this
to force it into some particular type--
not that any of this is good code.
I'm just saying that these are all things that are possible
in C++ 14.
And this becomes item number three now.
And moving on, variatic templates did not change in C++
14.
I will go ahead and delete that.
And that brings us to a new item, number four.
We had unique_ptr, but you might notice that we've
got this new int 5 right here.
C++ 14 gave us a handy utility called make_unique.
And I can do this, which then also gives me the extra ability
to stop repeating myself.
And I can say, please make this unique thing.
And it is going to be called pointer.
And it is a unique pointer to an integer, initialized
with the value of 5.
This was a definite oversight in the language.
And it is relatively easy, using variatic templates and fording
and move semantics that were added in C++ 11 to actually
give yourself a version of make_unique in C++ 11 if you
are so inclined.
But this make_unique fills a hole,
a hole in the language that is quite critical, because now,
as of C++ 14, we can say that we should probably never see any
new or delete at all explicitly written in our code.
We can use make_unique, make_shared.
I didn't mention make_shared in either the C++ 11 or the C++ 14
videos.
And I'm still not going to really dig into it,
because it is a very heavy hammer in C++ 11,
and it does have its uses.
It is an important tool for automatic memory management.
But in my opinion, make_unique and unique_ptr
are much more important to focus on.
Now, we had our constexpr in C++ 11.
But it gets much better in C++ 14.
So in C++ 14, if I wanted to--
this simple construct in C++ 11 would have been invalid.
In C++ 14, our constexpr gets much more flexible.
We can have loops.
We can have branches.
We can have multiple return statements.
We can do pretty much anything we want to inside
of a constexpr function in C++ 14,
except for call other functions that are not constexpr
in a non-constexpr context.
And really digging into that is going
to take more time than we're aiming for in this video.
But this is where we are so far with the top five features
of C++ 14.
So these are the things that I think really make C++ 14
important and different from C++ 11.
These are the things that you need to pay attention to if you
want to get the most out of C++ 14.
So again, thank you for watching this episode.
Be sure to subscribe.
And go and watch all of the other videos that I've got
on here on lambdas if you really want to learn more about them,
because the stuff that we can do with lambdas between C++ 14,
17, and 20, it gets kind of ridiculous.
But for the moment, we're just focusing on C++ 14--
really simplifies, unifies the language in many ways,
makes things much more consistent than they were.
So again, thanks for watching.
Hope you enjoyed this video.