Is there any reason to use the 'auto' keyword in C++03?
auto
is a storage class specifier, static
, register
and extern
too. You can only use one of these four in a declaration.
Local variables (without static
) have automatic storage duration, which means they live from the start of their definition until the end of their block. Putting auto in front of them is redundant since that is the default anyway.
I don't know of any reason to use it in C++. In old C versions that have the implicit int rule, you could use it to declare a variable, like in:
int main(void) { auto i = 1; }
To make it valid syntax or disambiguate from an assignment expression in case i
is in scope. But this doesn't work in C++ anyway (you have to specify a type). Funny enough, the C++ Standard writes:
An object declared without a storage-class-specifier at block scope or declared as a function parameter has automatic storage duration by default. [Note: hence, the auto specifier is almost always redundant and not often used; one use of auto is to distinguish a declaration-statement from an expression-statement (6.8) explicitly. — end note]
which refers to the following scenario, which could be either a cast of a
to int
or the declaration of a variable a
of type int
having redundant parentheses around a
. It is always taken to be a declaration, so auto
wouldn't add anything useful here, but would for the human, instead. But then again, the human would be better off removing the redundant parentheses around a
, I would say:
int(a);
With the new meaning of auto
arriving with C++0x, I would discourage using it with C++03's meaning in code.
What's the reason for auto keyword in pure C?
All variables are not auto
by default; anything declared at file scope is static
, for example.
The auto
keyword is a holdover from the BCPL and B languages, from which C was derived. It is largely vestigial at this point, but by that same token it doesn't do any harm, so there's no reason to get rid of it, either.
is it fine to use auto keyword in function parameter?
Until C++ 11 the auto
keyword was a "storage class specifier" whereas with C++ 11 it becomes a type-induction specifier.
To answer your question: depending on the C++ standard you use to compile your code, adjust the use of the auto
keyword accordingly. It's not portable across the pre/post C++ 11 boundary of the C++ standard.
C++ auto keyword. Why is it magic?
auto
was a keyword that C++ "inherited" from C that had been there nearly forever, but virtually never used because there were only two possible conditions: either it wasn't allowed, or else it was assumed by default.
The use of auto
to mean a deduced type was new with C++11.
At the same time, auto x = initializer
deduces the type of x
from the type of initializer
the same way as template type deduction works for function templates. Consider a function template like this:
template<class T>
int whatever(T t) {
// point A
};
At point A, a type has been assigned to T
based on the value passed for the parameter to whatever
. When you do auto x = initializer;
, the same type deduction is used to determine the type for x
from the type of initializer
that's used to initialize it.
This means that most of the type deduction mechanics a compiler needs to implement auto
were already present and used for templates on any compiler that even sort of attempted to implement C++98/03. As such, adding support for auto
was apparently fairly easy for essentially all the compiler teams--it was added quite quickly, and there seem to have been few bugs related to it either.
When this answer was originally written (in 2011, before the ink was dry on the C++ 11 standard) auto
was already quite portable. Nowadays, it's thoroughly portable among all the mainstream compilers. The only obvious reasons to avoid it would be if you need to write code that's compatible with a C compiler, or you have a specific need to target some niche compiler that you know doesn't support it (e.g., a few people still write code for MS-DOS using compilers from Borland, Watcom, etc., that haven't seen significant upgrades in decades). If you're using a reasonably current version of any of the mainstream compilers, there's no reason to avoid it at all though.
More recent revisions of the standard have added a few new places that auto
can be used. Starting with C++14, you can use auto
for the type of a parameter to a lambda:
[](auto s) { return s + 1; }
This does essentially the same thing as the example above--even though it doesn't explicitly use template
syntax, this is basically a template that deduces the type of the parameter, and instantiates the template over that type.
That was convenient and useful enough that in C++20, the same capability was added for normal functions, not just lambdas.
But, just as before all of this really comes down to using the same basic type deduction mechanism as we've had for function templates since C++98. auto
allows that to be used in more places, and more conveniently, but the underlying heavy lifting remains the same.
Where is the C auto keyword used?
auto
is a modifier like static
. It defines the storage class of a variable. However, since the default for local variables is auto
, you don't normally need to manually specify it.
This page lists different storage classes in C.
Goal of C's auto keyword
Bjarne Stroustrup mentions in his C++0x FAQ about auto
:
"The old meaning of auto ("this is a
local variable") is redundant and
unused. Several committee members
trawled through millions of lines of
code finding only a handful of uses --
and most of those were in test suites
or appeared to be bugs."
So I assume, that compilers wil not be forced by the standard to implement the old meaning of auto
.
Why do I need to explicitly write the 'auto' keyword?
Dropping the explicit auto
would break the language:
e.g.
int main()
{
int n;
{
auto n = 0; // this shadows the outer n.
}
}
where you can see that dropping the auto
would not shadow the outer n
.
Concept of auto keyword in c
auto
isn't a datatype. It's a storage class specifier, like static
. It's basically the opposite of static
when used on local variables and indicates that the variable's lifetime is equal to its scope (for example: when it goes out of scope it is automatically destroyed).
You never need to specify auto
as the only places you're allowed to use it it is also the default.
Related Topics
Is Reading an Indeterminate Value Undefined Behavior
Why Do We Actually Need Private or Protected Inheritance in C++
Cin.Ignore(Numeric_Limits<Streamsize>::Max(), '\N')
Lambda Expressions as Class Template Parameters
In C++11, Does 'I += ++I + 1' Exhibit Undefined Behavior
Questions About Name Mangling in C++
Create an Application Without a Window
Why Does C++ Not Have a Const Constructor
Is There an Iterator Across Unique Keys in a Std::Multimap
Split an Integer into Its Digits C++
Scope VS Life of Variable in C
What Are Declarations and Declarators and How Are Their Types Interpreted by the Standard
Should I Pass an Std::Function by Const-Reference
Are C++ Templates Just MACros in Disguise
Why Do I Need Double Layer of Indirection for MACros
What Happens When You Deallocate a Pointer Twice or More in C++