- [Presenter] The auto-type is relatively recent feature
in C++ beginning with C++11.
And it's quickly becoming very common.
This is auto dash type CPP
from chapter three of the exercise files.
Here we see that we can declare an object based
on the type of what it is being initialized with
and this is very convenient.
Keep in mind that it's still strongly typed,
it's just automatically so.
So if I look at the variable X here,
you see I'm declaring it as auto X,
and the function returns a standard string.
And so when I build and run this,
you see that the type of X is,
well, this very long thing here
and it'll be different if you're on a PC.
But basically that's the type of these standards string.
So I could have said standard string like this
and I get exactly the same result.
But I can also just say auto because the compiler knows
what the type return is from funk.
So with some very complex types
this can be very convenient.
Let's take a look at another example.
I'm going to go ahead
and I'm going to delete this auto-type.cpp
and I'm going to bring in auto-type to that CPP.
And here we have actually a more common example even,
here I have a vector and we'll talk about vectors
in the chapter on the STL, the standard template library.
But you notice that if I want to use an iterator
I have this rather complex looking type.
When I build and run this, I'm actually able to just,
you know, iterate through just like I would with a four loop
but I have to use this iterator in order to do it
and that's a rather complex type.
So instead, I can just type auto here.
And now all of a sudden this is more readable,
it's easy to tell what's going on,
I've got an iterator that's coming
from something that returns an iterator.
And when I build and run it works exactly the same.
But using auto instead of that longer definition
of the iterator type which is the way we used
to have to do it before C++11.
This is much more readable and more clear,
less prone to error, both in typing, and in reading.
So, this feature is a real improvement
and it's not without its dangerous.
Be careful to only use auto-types where
the implications are clear and the usage is concise.
Otherwise you could potentially sacrifice readability
and even introduce errors that could be hard to find.