C++ auto Keyword
In this page:
auto variable_name = value; // type is deduced from value
auto *pointer_name = &variable;
auto &reference_name = variable;
Type Inference Basics
The auto keyword tells the compiler to deduce a variable's type from its initializer, rather than you spelling it out explicitly.
auto x = 5; makes x an int just as if you'd written int x = 5; -- the type isn't dynamic or looser, it's determined once at compile time and fixed from then on.
Example: Type Inference Basics
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
auto x = 5;
// Print to the console with cout
std::cout << x << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
auto with Complex Types
auto can be combined with * and & to control exactly what's deduced -- auto* p requires the initializer to be a pointer, and auto& deduces a reference type, which matters when the difference between a copy and a reference changes program behavior.
Example: auto with Complex Types
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare value and set it to 10
int value = 10;
auto *p = &value;
auto &ref = value;
// Print to the console with cout
std::cout << *p << " " << ref << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
auto with Loops
Range-based for loops are one of the most common places to see auto, since spelling out a container's full iterator or element type (like std::vector<std::pair<std::string,int>>::iterator) is often long and easy to get wrong -- for (auto& item : container) sidesteps that entirely.
Example: auto with Loops
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {1, 2, 3};
// Range-based for loop: visit every element of nums
for (auto n : nums) std::cout << n << " ";
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
auto for Return Types
Declaring a function's return type as auto lets the compiler infer it from the return statement inside the function body, useful for template functions where the exact return type would otherwise be hard to write out or depend on the template parameters.
Example: auto for Return Types
// Include std::cout and std::cin
#include <iostream>
auto add(int a, int b) {
// Send a + b back to the caller
return a + b;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << add(2, 3) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
auto with Lambdas
Lambda expressions are usually stored in auto-declared variables since a lambda's actual type is a compiler-generated, unnameable closure type -- you literally cannot write out its type by hand. auto is also used for generic lambda parameters ([](auto x, auto y)), letting one lambda accept multiple argument types.
Example: auto with Lambdas
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Define a lambda (anonymous function) stored in square
auto square = [](int x) { return x * x; };
// Print to the console with cout
std::cout << square(5) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Declaring
auto x;with no initializer, which cannot deduce a type. - Expecting
autoto keep references or const, when it drops them unless you writeauto&. - Overusing
autoso the type is unclear to readers.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: