Semicolon in C++?

后端 未结 9 1583
心在旅途
心在旅途 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: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.

提交回复
热议问题