• clang and gcc are not converging on constexpr

    From Thiago Adams@thiago.adams@gmail.com to comp.lang.c on Thu Nov 7 16:16:49 2024
    From Newsgroup: comp.lang.c


    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






    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Louis Krupp@lkrupp@invalid.pssw.com.invalid to comp.lang.c on Fri Nov 8 03:23:21 2024
    From Newsgroup: comp.lang.c

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Thiago Adams@thiago.adams@gmail.com to comp.lang.c on Fri Nov 8 09:04:13 2024
    From Newsgroup: comp.lang.c

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Louis Krupp@lkrupp@invalid.pssw.com.invalid to comp.lang.c on Sat Nov 9 00:42:23 2024
    From Newsgroup: comp.lang.c

    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



    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Opus@ifonly@youknew.org to comp.lang.c on Mon Nov 11 02:24:16 2024
    From Newsgroup: comp.lang.c

    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.

    --- Synchronet 3.20a-Linux NewsLink 1.114