Discussion:
clang and gcc are not converging on constexpr
(too old to reply)
Thiago Adams
2024-11-07 19:16:49 UTC
Permalink
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
Louis Krupp
2024-11-08 10:23:21 UTC
Permalink
Post by Thiago Adams
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
Thiago Adams
2024-11-08 12:04:13 UTC
Permalink
Post by Louis Krupp
Post by Thiago Adams
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
Louis Krupp
2024-11-09 07:42:23 UTC
Permalink
Post by Thiago Adams
Post by Louis Krupp
Post by Thiago Adams
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
Opus
2024-11-11 01:24:16 UTC
Permalink
Post by Louis Krupp
Post by Thiago Adams
Post by Louis Krupp
Post by Thiago Adams
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++.
===
sa1.c:3:24: error: expression in static assertion is not constant
    3 |     static_assert(a[0] == 1);
      |                   ~~~~~^~~~
~~~~^~~~
===
===
               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)
===
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.

Loading...