Semicolon in C++?

后端 未结 9 1584
心在旅途
心在旅途 2020-12-16 11:07

Is the \"missing semicolon\" error really required? Why not treat it as a warning?

When I compile this code

int f = 1
int h=2;

the

相关标签:
9条回答
  • 2020-12-16 11:29

    It's very good that the C++ compiler doesn't do this. One of the worst aspects of JavaScript is the semicolon insertion. Picture this:

    return
      (a + b);
    

    The C++ compiler will happily continue on the next line as expected, while a language that "inserts" semicolons, like JavaScript, will treat it as "return;" and miss out the "(a + b);".

    Instead of rely on compiler error-fixing, make it a habit to use semicolons.

    0 讨论(0)
  • 2020-12-16 11:35

    First, this is only a small example; are you sure the compiler can intelligently tell you what's wrong for more complex code? For any piece of code? Could all compilers intelligently recognize this in the same way, so that a piece of C++ code could be guaranteed portable with missing semicolons?

    Second, C++ was created more than a decade ago when computing resources aren't nearly what they are now. Even today, builds can take a considerable amount of time. Semicolons help to clearly demarcate different commands (for the user and for the compiler!) and assist both the programmer and the compiler in understanding what's happening.

    0 讨论(0)
  • 2020-12-16 11:35

    I won't extend much of the need for semi-colon vs line continuation characters, both have advantages and disadvantages and in the end it's a simple language design choice (even though it affects all the users).

    I am more worried about the suggestion for the compiler to fix the code.

    If you have ever seen a marvelous tool (such as... hum let's pick up a merge tool) and the way it does its automated work, you would be very glad indeed that the compiler did not modify the code. Ultimately if the compiler knew how to fix the code, then it would mean it knew your intent, and thought transmission has not been implemented yet.

    As for the warning ? Any programmer worth its salt knows that warnings should be treated as errors (and compilation stopped) so what would be the advantage ?

    0 讨论(0)
  • 2020-12-16 11:36

    In C programs semicolons are statement terminators, not separators. You might want to read this fun article.

    0 讨论(0)
  • 2020-12-16 11:42

    Having semi-colons (or line breaks, pick one) makes the compiler vastly simpler and error messages more readable.

    But contrary to what other people have said, neither form of delimiters (as an absolute) is strictly necessary.

    Consider, for example, Haskell, which doesn’t have either. Even the current version of VB allows line breaks in many places inside a statement, as does Python. Neither requires line continuations in many places.

    For example, VB now allows the following code:

    Dim result = From element in collection
                 Where element < threshold
                 Select element
    

    No statement delimiters, no line continuations, and yet no ambiguities whatsoever.

    Theoretically, this could be driven much further. All ambiguities can be eliminated (again, look at Haskell) by introducing some rules. But again, this makes the parser much more complicated (it has to be context sensitive in a lot of places, e.g. your return example, which cannot be resolved without first knowing the return type of the function). And again, it makes it much harder to output meaningful diagnostics since an erroneous line break could mean any of several things so the compiler cannot know which error the user has made, and not even where the error was made.

    0 讨论(0)
  • 2020-12-16 11:44

    There are many cases where a semicolon is needed.

    What if you had:

    int *y;
    int f = 1
    *y = 2;
    

    This would be parsed as

    int *y;
    int f = 1 * y = 2;
    

    So without the semicolons it is ambiguous.

    0 讨论(0)
提交回复
热议问题