Liste des Groupes | Revenir à cl c |
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.
Les messages affichés proviennent d'usenet.