Sujet : Re: C23 thoughts and opinions
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 23. May 2024, 22:49:24
Autres entêtes
Organisation : None to speak of
Message-ID : <87ed9s42sb.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Thiago Adams <
thiago.adams@gmail.com> writes:
On 23/05/2024 10:11, David Brown wrote:
On 23/05/2024 14:38, Thiago Adams wrote:
[...]
I already see bad usages of constexpr in C++ code. It was used in
cases where we know for sure that is NOT compile time. This just
make review harder "why did someone put this here?" conclusion was
it was totally unnecessary and ignored by the compiler. The
programmer was trying to add something extra, like "magic" hoping
for something that would never happen.
>
IME poor or confusing uses of "constexpr" are for functions, not
objects, and C23 does not support "constexpr" for functions.
>
The sample C++ was something like
>
constexpr char * s[] = {"a", "b"};
for (int i = 0; i < sizeof(s); i++)
{
//using s[i]
}
>
I checked in C, it is an error.
Apparently C23 has stricter rules for constexpr than C++ does. I can
imagine those rules being relaxed in future editions of the C standard.
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 compile that with g++ 14.1.0, with -std=c++23, I get:
warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
When I replace "char*" by "const char*", that warning goes away.
The initializer *can* be evaluated at compile time. Using constexpr
catches any changes to the initializer that would require run-time code
execution.
Feel free to redirect to comp.lang.c++ if you want to discuss the C++
aspects further.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */