Sujet : Re: C23 thoughts and opinions
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 24. May 2024, 21:19:16
Autres entêtes
Organisation : None to speak of
Message-ID : <87v8330xq3.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Thiago Adams <
thiago.adams@gmail.com> writes:
On 24/05/2024 16:45, Keith Thompson wrote:
Thiago Adams <thiago.adams@gmail.com> writes:
On 23/05/2024 18:49, Keith Thompson wrote:
error: 'constexpr' pointer initializer is not null
5 | constexpr char * s[] = {"a", "b"};
>
>
Then we were asking why constexpr was used in that case.
Why not?
>
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.
I don't understand. Do you object because it's not *immediately
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.
Your understanding is incorrect. "constexpr" is not a mere hint.
In C23, an object defined with "constexpr" must be initialized with a
constant expression. C++ has similar rules. Attempting to initialize
it with a non-constant expression requires a diagnostic.
int non_const = 42;
constexpr int bad = non_const; // invalid, must be diagnosed
I'd be surprised to see a C or C++ compiler that failed to diagnose such
an error. Have you run into this, or are you speculating?
Given that the C++ code
constexpr const char * s[] = {"a", "b"}; // I added "const"
compiles successfully, you can safely assume that the initializer can be
and is evaluated at compile time.
C++ has consteval
(https://en.cppreference.com/w/cpp/language/consteval) . When I first
saw it I thought it was a joke.
And I know just the newsgroup where it can be discussed.
[...]
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */