I don't believe such a possibility was ever considered by the C++ committee.
Further, although FORTRAN did have a sort-of implicit type system (variables starting with 'I' through 'N' were integers, everything else was real) I can't quite see how such a thing would work in a block-structured language like C++. For example, consider code like this:
int f() {
int i;
if (something) {
i = 2.0;
In C or C++ as currently defined the meaning of this is quite clear: the i = 2.0
is taking 2.0
, converting it to int
, and then assigning the resulting 2
to to the i
that was declared at the outer scope.
If, however, you allowed the auto
to be omitted, this would apparently define a new variable in the scope of the block controlled by the if
statement, and that variable would (apparently) have type double
, and the i
at the outer scope would retain its previous value.
Now, I'll openly admit that depending on the implicit conversion from double
to int
here is probably a bad idea--but it's been part of the language for so long (decades) that it's effectively impossible to change it now.
In short, if you allowed the auto
to be omitted, you'd almost certainly break some unknown (but almost certainly quite large) amount of existing code. Given the importance of backward compatibility in C++, this would be sufficiently unacceptable that I can't imagine the committee could ever give it any serious consideration.
auto
keyword signals the compiler to use type inference to determine the type. Are you asking why we need theauto
keyword at all? Because in C++ variable declarations are always preceded by something; otherwise the equals sign you use in your statement would be interpreted as an assignment, not a declaration.let
; Go uses:=
; C#, Swift, Scala, Typescript, and Dart usesvar
; Scala also usesval
. I think it is easier to parse if you put something before a variable declaration.