C++11 brought a lot of features in C++ to make the language easier to use, more efficient, and better suited for modern programming tasks. One of these features is the auto
keyword for type inference. The addition of auto
allows developers to write less code, reduce redundancy, and maintain code more easily, especially when dealing with complex data types or templates. This article will look into the uses of auto
, how to leverage it, and important points to remember when using it.
What is auto Keyword in C++11?
Introduced in C++11, the auto
keyword enables developers to declare a variable without explicitly specifying its data type. Instead, the compiler deduces the type of the variable from the initialization expression. This feature is quite handy, as it simplifies the code and makes it more readable, especially when the data type is complex or obvious from the context.
Declaring Variables with auto
Here’s how you can use auto
to declare variables:
// Storing an int in an auto variable auto var_1 = 5; // Storing a character in an auto variable auto var_2 = 'C'; std::cout << var_1 << std::endl; std::cout << var_2 << std::endl;
In the above example, var_1
is deduced to be of type int
and var_2
as type char
.
Using auto with Different Types
The auto
keyword is versatile and can be used with any type, including functions or iterators. For instance, you can store a lambda function in an auto
variable like so:
// Storing a Lambda function in an auto variable auto fun_sum = [](int a, int b) { return a + b; }; std::cout << fun_sum(4, 5) << std::endl;
Simplifying Complex Type Declarations
One of the main advantages of auto
is when you’re dealing with types that are cumbersome to write out. Consider a std::map
with std::string
keys and values:
std::map<std::string, std::string> mapOfStrs; // Inserting data into the map mapOfStrs.insert({"first", "1"}); mapOfStrs.insert({"sec", "2"}); mapOfStrs.insert({"third", "3"});
To iterate over this map and display its contents, you traditionally need to declare an iterator like this:
// Iterating over the map and displaying all data for (std::map<std::string, std::string>::iterator it = mapOfStrs.begin(); it != mapOfStrs.end(); ++it) { std::cout << it->first << "::" << it->second << std::endl; }
However, with auto
, this becomes much simpler:
// Iterating using auto for (auto itr = mapOfStrs.begin(); itr != mapOfStrs.end(); ++itr) { std::cout << itr->first << "::" << itr->second << std::endl; }
Important Points About auto
in C++11
Immutability of Type
Once you have initialized an auto
variable, you can change its value but not its type:
auto x = 1; // This will cause a compile-time error because 'x' is of type int // x = "dummy";
Requirement of Initialization
An auto
variable cannot be left uninitialized because its type is deduced from the initializer:
// This will cause a compile-time error // auto a;
Returning auto from a Function
When you want to return an auto
variable from a function, you use a trailing return type:
auto sum(int x, int y) -> int { return x + y; } // Calling the function that returns 'auto' auto value = sum(3, 5);
In this example, auto
is used to specify that the return type of the function sum
is the same as the type of the expression in the return statement. The -> int
is a trailing return type specifying that the return type is int
.
Summary
The auto
keyword is a powerful feature in C++11 that simplifies code and makes it more adaptable. It reduces the verbosity of complex type declarations, thus making the code cleaner and easier to maintain. While it’s tempting to use auto
everywhere, it’s important to remember that explicit type declaration can sometimes make the code more understandable, especially when the type isn’t immediately clear from the context. Use auto
judiciously to strike the right balance between brevity and clarity in
Extraordinary Tutorials for C++. Excellent explanation. Good work guys keep it up.
Hats off guys. Really a wonderful job this is!
I made notes from, your 1st video tutorial on memory management and that clears my complete doubt!
Now you added C++ 11/14 features in detail.
I am requesting you to add a tutorial as well like you had done on memory management!
Thanks for your kind words