1

When auto was considered for addition to the C++ standard, was there any discussion of setting implicit types where there are no explicit declarations? And if so, was the discussion recorded anywhere?

Not intended as a criticism. Just curious from a PL perspective.

3
  • 1
    I don't understand your question. The auto keyword signals the compiler to use type inference to determine the type. Are you asking why we need the auto 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. Commented Jul 14, 2016 at 15:37
  • 3
    To add to that, Rust and Swift uses let; Go uses :=; C#, Swift, Scala, Typescript, and Dart uses var; Scala also uses val. I think it is easier to parse if you put something before a variable declaration. Commented Jul 14, 2016 at 15:52
  • 1
    @Phyllostachys It's not so much that it's easier to parse, but simply put you can't catch misspelt variable names if you implicitly declare them
    – Jules
    Commented Jul 15, 2016 at 15:02

4 Answers 4

7

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.

9

In 1963, Tony Hoare proposed adding implicit type rules to ALGOL. The ALGOL committee boxed his ears, HARD. Requiring variables to be declared explicitly was known, EVEN THEN, shown to reduce errors in programming.

Tony mentioned this in his Turing talk, and said it was BEFORE the probably-apocryphal Venus probe FORTRAN story, where a typo in a FORTRAN DO-statement instead created a brand-new variable and assigned it a value. Various versions of that story float around: the most credible one says that it was caught in a review, and, if it had not been caught, would have resulted in the total loss of a (very expensive) spacecraft.

This was 53 years ago.

5

Allowing optional types where they used to be required can make the syntax ambiguous. It is easier to add a keyword (or to reuse one in this case) than changing the grammar.

4

The C++ syntax is designed in such a way that a declaration always needs a type to distinguish it from assignment. You simply cannot leave out the type, because that already has a different meaning:

int i = 1;
// Leaving out the type turns it into assignment:
i = 1;

int i;
// Leaving out the type turns it into evaluation:
i;

You simply need an explicit type annotation that tells the compiler that you don't want an explicit type. Sounds crazy, but that's what it is.

Other languages' syntax is designed differently, e.g. in Scala:

val i: Int = 10
// without type:
val i = 10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.