Liste des Groupes | Revenir à cl c |
Em 5/24/2024 9:46 PM, Keith Thompson escreveu:That is disappointing. I too would have expected that to work in C23. My guess is that it is the implicit pointer dereference that is the problem. But I hope this is something that gets fixed shortly.Thiago Adams <thiago.adams@gmail.com> writes:Something used to produce a constant expression.Em 5/24/2024 5:19 PM, Keith Thompson escreveu:>Thiago Adams <thiago.adams@gmail.com> writes:I think I can explain I little betterOn 24/05/2024 16:45, Keith Thompson wrote:Your understanding is incorrect. "constexpr" is not a mere hint.Thiago Adams <thiago.adams@gmail.com> writes:>On 23/05/2024 18:49, Keith Thompson wrote:I don't understand. Do you object because it's not *immediately>error: 'constexpr' pointer initializer is not nullWhy not?
5 | constexpr char * s[] = {"a", "b"};
>
>
Then we were asking why constexpr was used in that case.
When I see a constexpr I ask if the compiler is able to compute
everything at compile time. If not immediately it is a bad usage in my
view.
obvious* that everthing can be computed at compile time? If so, why
should it have to be?
My understanding is that constexpr is a tip for the compiler. Does not
ensure anything. Unless you use where constant expression is required.
So I don't like to see constexpr where I know it is not a constant
expression.
>
Let´s consider we have a compile time array of integers and a loop.
>
https://godbolt.org/z/e8cM1KGWT
>
#include <stdio.h>
#include <stdlib.h>
int main() {
constexpr int a[] = {1, 2, 3, 4, 5, 6, 7, 8};
for (int i = 0 ; i < sizeof(a)/sizeof(a[0]); i++)
{
printf("%d", a[i]);
}
}
>
What the programmer expected using a constant array in a loop?
The loop is in runtime, unless the compiler expanded the loop into 8
calls using constant expressions. But this is not the case.
This was the usage of constexpr I saw but with literal strings.
So, the array a is not used as constant even if it has constexpr.
What do you mean by "used as constant"?
>
In the loop the compiler would have to get the value in runtime from array, or unroll the loop.
I just checked, trying to extract an constant value from the array
https://godbolt.org/z/v33Pqd7W8
#include <stdio.h>
#include <stdlib.h>
int main() {
constexpr int a[] = {1, 2, 3, 4, 5, 6, 7, 8};
static_assert(a[0] ==1 );
}
I was expecting this to work!
But gcc says
<source>:5:24: error: expression in static assertion is not constant
5 | static_assert(a[0] ==1 );
|
The mess is even bigger than I thought.
In c++ it works
https://godbolt.org/z/qG6vGhEMj
Les messages affichés proviennent d'usenet.