Skip to main content

The auto Keyword

In C++, the auto keyword is used for type inference, allowing the compiler to automatically deduce the type of a variable at compile-time. This can make your code more readable and maintainable. However, you can also use auto with pointers (auto*) and references (auto&) to indicate that you want a pointer or reference to a deduced type. Here's a breakdown:

auto​

When you use auto, the compiler deduces the type of the variable based on its initializer.

auto x = 42; // int
auto y = 3.14; // double
auto z = "hello"; // const char*

auto*​

When you use auto*, you're telling the compiler that you want a pointer to the type that is being deduced. However, if the type being deduced is already a pointer, then using auto* would be redundant.

int a = 42;
auto* p = &a; // int*, but 'auto' alone would suffice

double b = 3.14;
auto* q = &b; // double*, but 'auto' alone would suffice

auto&​

When you use auto&, you're telling the compiler that you want a reference to the type that is being deduced.

int c = 42;
auto& r = c; // int&

double d = 3.14;
auto& s = d; // double&

Function Return Types and auto​

When dealing with functions that return pointers or references, auto alone is often sufficient to capture the full type, including the pointer or reference part.

For example:

MyClass* MyFunction() {
// Function implementation here
return nullptr; // Just for demonstration
}

auto myclass = MyFunction(); // myclass is of type MyClass*

In this case, auto captures the full type MyClass*, making auto* redundant:

auto* myclass = MyFunction(); // myclass is of type MyClass*, but the '*' is redundant

Both auto and auto* would result in myclass having the type MyClass* in this specific example.

In summary, auto, auto*, and auto& are powerful features in C++ that allow for type inference, making your code more concise and often easier to maintain. The use of * and & with auto is generally redundant when the type being deduced already includes these qualifiers.