• 7.1.4p2 vs. Annex J

    From Keith Thompson@21:1/5 to All on Mon Aug 3 20:42:45 2020
    Joe Zbiciak and I discovered this in a discussion on Quora.

    N1570 7.1.4p2 says:

    Provided that a library function can be declared without reference
    to any type defined in a header, it is also permissible to declare
    the function and use it without including its associated header.

    which means that this is a valid "Hello, world" program:

    int puts(const char *s);

    int main(void) {
    puts("Hello, world");
    }

    But J.2 (which is non-normative) says that the behavior is undefined if:

    A function, object, type, or macro that is specified as being
    declared or defined by some standard header is used before any
    header that declares or defines it is included (7.1.2).

    which the above program violates.

    Is the intent behind that paragraph in J.2 something other than
    what it seems to be? (Or am I misreading it?) Or is it just wrong?

    Other than declaring a function, as permitted by 7.1.4p2, how would
    a program even violate that paragraph in J.2 without also violating
    a constraint?

    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    Working, but not speaking, for Philips Healthcare
    void Void(void) { Void(); } /* The recursive call of the void */

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ike Naar@21:1/5 to Keith Thompson on Tue Aug 4 05:26:51 2020
    On 2020-08-04, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
    Joe Zbiciak and I discovered this in a discussion on Quora.

    N1570 7.1.4p2 says:

    Provided that a library function can be declared without reference
    to any type defined in a header, it is also permissible to declare
    the function and use it without including its associated header.

    which means that this is a valid "Hello, world" program:

    int puts(const char *s);

    int main(void) {
    puts("Hello, world");
    }

    But J.2 (which is non-normative) says that the behavior is undefined if:

    A function, object, type, or macro that is specified as being
    declared or defined by some standard header is used before any
    header that declares or defines it is included (7.1.2).

    which the above program violates.

    The header that declares the puts() function is <stdio.h>.
    The above program does not include <stdio.h>.
    So the puts() function is not used before <stdio.h> is included.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Philipp Klaus Krause@21:1/5 to All on Tue Aug 4 10:53:41 2020
    J.2 is informative, and the part you quoted refers to normative text
    7.1.2. In the current C2X draft N2479, the relevant sentenceg in 7.1.2 is:

    "If used, a header shall be included outside of any external declaration
    or definition, and it shall first be included before the first reference
    to any of the functions or objects it declares, or to any of the types
    or macros it defines."

    So clearly, the undefined behaviour does not happen when the header is
    not used at all.

    Philipp

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Keith Thompson@21:1/5 to Philipp Klaus Krause on Tue Aug 4 07:29:28 2020
    Philipp Klaus Krause <pkk@spth.de> writes:
    J.2 is informative, and the part you quoted refers to normative text
    7.1.2. In the current C2X draft N2479, the relevant sentenceg in 7.1.2 is:

    "If used, a header shall be included outside of any external declaration
    or definition, and it shall first be included before the first reference
    to any of the functions or objects it declares, or to any of the types
    or macros it defines."

    So clearly, the undefined behaviour does not happen when the header is
    not used at all.

    Please quote (some of) the article you're replying to.

    In N2479, J.2 still says the behavior is undefined if:

    - A function, object, type, or macro that is specified as being
    declared or defined by some standard header is used before any
    header that declares or defines it is included (7.1.2).

    The normative text is clear enough in both N1570 and N2479. The text in
    J.2 is at best unclear. That's the error I'm referring to.

    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    Working, but not speaking, for Philips Healthcare
    void Void(void) { Void(); } /* The recursive call of the void */

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andy Walker@21:1/5 to Keith Thompson on Wed Aug 5 10:47:04 2020
    On 04/08/2020 04:42, Keith Thompson wrote:
    But J.2 (which is non-normative) says that the behavior is undefined if:
    A function, object, type, or macro that is specified as being
    declared or defined by some standard header is used before any
    header that declares or defines it is included (7.1.2).

    This is a fine example of the ambiguity in English that makes
    writing standards therein fraught with difficulties. The structure is
    "UB if A before B". Keith reads that as forbidding "A" without "B",
    the same structure as "you can't go out before Saturday"; others read
    it as "A" being allowed by itself or following "B", but not if there
    is a later "B", the same structure as "you can't eat before swimming".
    So it's ambiguous.

    I used to use "while" to illustrate a similar ambiguity; the
    UK used to have a road sign "wait here while lights are red" in front
    of temporary signals for road works. But whereas most people read
    "while" as meaning "for as long as" [also the programming usage],
    in parts of the UK it means "until" ["wait here while the doctor
    comes"], which left locals [allegedly] waiting on the green light
    and driving through on red [even in Yorkshire, they're probably
    not that daft, though it might be an ingenious excuse following an
    accident].

    This article is brought to you from the campaign for formal
    definitions of languages. [There has been a relevant thread over
    in "comp.lang.misc".]

    --
    Andy Walker,
    Nottingham.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)