• Re: clang and gcc are not converging on constexpr

    From Opus@21:1/5 to Louis Krupp on Mon Nov 11 02:24:16 2024
    On 09/11/2024 08:42, Louis Krupp wrote:
    On 11/8/2024 5:04 AM, Thiago Adams wrote:
    Em 11/8/2024 7:23 AM, Louis Krupp escreveu:
    On 11/7/2024 12:16 PM, Thiago Adams wrote:

    The differences relate to arrays. I think the standard leaves some
    flexibility in the specification, so there may not be a strict right
    or wrong - just different approaches. The challenge for creating
    portable code is knowing when it will work consistently across
    different compilers.

    Sample

    int main() {
        constexpr int a[] = {1, 2};
        static_assert(a[0] == 1);
    }

    works in clang but not in gcc


    Could it be your version of gcc? The program compiles and runs with
    gcc (GCC) 14.2.1 20240912 (Red Hat 14.2.1-3) on Fedora 40.

    Louis

    I was comparing against this (trunk)

    https://godbolt.org/z/z88ec3K8E

    -v show 15 something

    My mistake: I'd used a .cxx extension for the source file, and gcc
    apparently compiled it with g++.

    I saw that you were using -std=c23, so I tried that, and I got this:

    ===
    sa1.c: In function ‘main’:
    sa1.c:3:24: error: expression in static assertion is not constant
        3 |     static_assert(a[0] == 1);
          |                   ~~~~~^~~~
    ~~~~^~~~
    ===

    The man page for gcc had this to say about -std=c23:

    ===
                   ISO  C23,  the  2023  revision  of  the ISO C standard
    (expected to be published in
                   2024).  The support for this version is experimental and
    incomplete.
    ===

    My currently installed version of clang -- 18.1.8 (Fedora 18.1.8-1.fc40)
    -- doesn't like the code, with or without -std=c23:

    ===
    sa1.c:2:5: error: use of undeclared identifier 'constexpr'
        2 |     constexpr int a[] = {1, 2};
          |     ^
    sa1.c:3:19: error: use of undeclared identifier 'a'
        3 |     static_assert(a[0] == 1);
          |                ^
    ===

    My guess is that gcc and clang will match eventually, but we're not
    there yet.

    Probably. The fact that gcc doesn't seem to consider a[0] a constant
    expression here doesn't look good. Kind of makes constexpr pointless. So
    let's hope it's just only partially supported so far and that it won't
    be considered valid, because to me it isn't.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Louis Krupp@21:1/5 to Thiago Adams on Fri Nov 8 03:23:21 2024
    On 11/7/2024 12:16 PM, Thiago Adams wrote:

    The differences relate to arrays. I think the standard leaves some flexibility in the specification, so there may not be a strict right
    or wrong - just different approaches. The challenge for creating
    portable code is knowing when it will work consistently across
    different compilers.

    Sample

    int main() {
        constexpr int a[] = {1, 2};
        static_assert(a[0] == 1);
    }

    works in clang but not in gcc


    Could it be your version of gcc? The program compiles and runs with gcc
    (GCC) 14.2.1 20240912 (Red Hat 14.2.1-3) on Fedora 40.

    Louis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Louis Krupp@21:1/5 to Thiago Adams on Sat Nov 9 00:42:23 2024
    On 11/8/2024 5:04 AM, Thiago Adams wrote:
    Em 11/8/2024 7:23 AM, Louis Krupp escreveu:
    On 11/7/2024 12:16 PM, Thiago Adams wrote:

    The differences relate to arrays. I think the standard leaves some
    flexibility in the specification, so there may not be a strict right
    or wrong - just different approaches. The challenge for creating
    portable code is knowing when it will work consistently across
    different compilers.

    Sample

    int main() {
        constexpr int a[] = {1, 2};
        static_assert(a[0] == 1);
    }

    works in clang but not in gcc


    Could it be your version of gcc? The program compiles and runs with
    gcc (GCC) 14.2.1 20240912 (Red Hat 14.2.1-3) on Fedora 40.

    Louis

    I was comparing against this (trunk)

    https://godbolt.org/z/z88ec3K8E

    -v show 15 something

    My mistake: I'd used a .cxx extension for the source file, and gcc
    apparently compiled it with g++.

    I saw that you were using -std=c23, so I tried that, and I got this:

    ===
    sa1.c: In function ‘main’:
    sa1.c:3:24: error: expression in static assertion is not constant
        3 |     static_assert(a[0] == 1);
          |                   ~~~~~^~~~
    ~~~~^~~~
    ===

    The man page for gcc had this to say about -std=c23:

    ===
                   ISO  C23,  the  2023  revision  of  the ISO C standard
    (expected to be published in
                   2024).  The support for this version is experimental and
    incomplete.
    ===

    My currently installed version of clang -- 18.1.8 (Fedora 18.1.8-1.fc40)
    -- doesn't like the code, with or without -std=c23:

    ===
    sa1.c:2:5: error: use of undeclared identifier 'constexpr'
        2 |     constexpr int a[] = {1, 2};
          |     ^
    sa1.c:3:19: error: use of undeclared identifier 'a'
        3 |     static_assert(a[0] == 1);
          |                ^
    ===

    My guess is that gcc and clang will match eventually, but we're not
    there yet.

    Louis

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