Search code examples
c++language-lawyerstandardswell-formed

In the C++ standard does well-formed means that the code compiles?


The C++ standards defines well-formed programs as

C ++ program constructed according to the syntax rules, diagnosable semantic rules, and the one-definition rule

I am wondering if all well-formed program compile or not (if it is not the case, what types of error make the difference between a well-formed program and a compilable problem). For example would a program containing ambiguity errors considered as well-formed?


Solution

  • A well-formed program can have undefined behaviour.

    It's in a note, and thus not technically authoritative, but it seems that it is intention that termination of compilation (or "translation" as the standard calls it) is within the scope of possible UB:

    [intro.defs]

    undefined behavior

    behavior for which this document imposes no requirements
    [ Note: Undefined behavior may be expected when this document omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data.

    Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

    Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed.

    Evaluation of a constant expression never exhibits behavior explicitly specified as undefined in [intro] through [cpp] of this document ([expr.const]). — end note ]


    There are also practical implementation limits:

    [implemits]

    Because computers are finite, C++ implementations are inevitably limited in the size of the programs they can successfully process.
    Every implementation shall document those limitations where known. This documentation may cite fixed limits where they exist, say how to compute variable limits as a function of available resources, or say that fixed limits do not exist or are unknown.


    Furthermore, compilers can have, and do have bugs. Well-formed simply means the a standard conforming compiler should compile it (within the limitations mentioned above). A buggy compiler does not necessarily conform to the standard.


    Lastly, the standard document itself is not perfect. If there is disagreement about what the rules mean, then it is possible for a program to be well-formed under one interpretation, and ill-formed under another interpretation.

    If a compiler disagrees with the programmer or another compiler, then it might fail to compile a program that is believed to be well-formed by the other party.