C++ Auto Keyword

The C++ Auto keyword is a handy feature that “automatically” determines the appropriate data type required at run time. It uses the initializing value, to determine the datatype that it must adopt. And of course, this means we don’t have to write extra code, and instead use the auto keyword for a variety of different types.

The Auto Keyword

Let’s begin with some simple uses of Auto in C++. In the Code below we can see that we are using auto to declare the type for variable x.

int main() {
    auto x = 5;

What happens here, is that at compile time, the data type of assigning value (5) is detected as int, and auto adopts the datatype of int, giving variable x the datatype int.

The below code expands upon the previous example, showing multiple operations and assignments.

int main() {
    auto x = 5;
    auto y = 3;

    auto z = x + y;
    cout << z << endl;

Try running this code for yourself!

Auto with Functions

Besides simple variables, we can use Auto on Functions as well. We can use it within the functions, as parameters or as the return type for the function.

void print(auto arg) {
    cout << arg << endl;

int main() {
    float x = 9.221;

    print("Hello World!");

This actually allows us to create type independent code, as that one print statement we wrote up there, works for any datatype that you pass to it.

Let’s look at another example, where we have the inc() function, meant to be used for numerical data types.

auto inc(auto value) {
    return value + 1;

int main() {
    cout << inc(3) << endl;
    cout << inc(7.48) << endl;

Regardless of the type, the numerical value will get incremented, and returned as the same type back into the main program.



Auto with For-Each

The for loop in C++ has another variant called the For-each loop. It’s a very handy way of iterating over Arrays or other containers types due to it’s simplicity.

Now remember while using for loops you need to specify a datatype. This restricts the use of the for loop to Arrays/Containers of that Datatype. However, using the auto keyword, we are able to making it type generic. This allows it to iterate over containers of any type, even custom ones.

int main() {
    int arr[5] = {1, 3, 2, 8, 4};

    for (auto i: arr) {
        cout << i << " ";

You can learn more about the For-Each loop in our separate dedicated tutorial for it.

Auto with Containers

Another one of the Auto Keyword’s best uses is with containers and template types.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> container = { 3, 8, 1, 2};

    for(auto i = container.begin(); i != container.end(); ++i) {
        cout << *i << endl;

Similar to how it works with For-Each, the Auto Keyword helps us write type generic code here as well.

Auto is a powerful tool, but try not to get too excessive with it. Over use of the Auto keyword can actually hurt readability, so be careful.

This marks the end of the C++ Auto Keyword Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Notify of
Inline Feedbacks
View all comments