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
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
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
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.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 991 |
Nodes: | 10 (0 / 10) |
Uptime: | 119:55:47 |
Calls: | 12,958 |
Files: | 186,574 |
Messages: | 3,265,641 |