Deutsch   English   Français   Italiano  
<veg59o$kolq$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bart <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: constexpr keyword is unnecessary
Date: Sun, 13 Oct 2024 10:52:55 +0100
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <veg59o$kolq$1@dont-email.me>
References: <veb5fi$3ll7j$1@dont-email.me> <vedv0a$5m19$1@dont-email.me>
 <veeqhi$ar0c$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 13 Oct 2024 11:52:56 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="b6496c9b8e6f9ecec99d92ab77d070d8";
	logging-data="680634"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19TNIvNnfBzpPDs4TtsiOj1"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:y3woxD07V5cW6jKnHgr5t7xyF6E=
Content-Language: en-GB
In-Reply-To: <veeqhi$ar0c$2@dont-email.me>
Bytes: 2292

On 12/10/2024 22:43, Thiago Adams wrote:
> Em 10/12/2024 10:53 AM, Bart escreveu:
>>
>> It depends on what constexpr means.
> 
> It means the initialization expression must be evaluated at compilation.
> Also the declarator can be used in another expression as constant.
> 
> Sample
> constexpr int a = 1;
> constexpr int b = a+1;
> 
> But the expression is always something the compiler need to check if is 
> constant or not.
> 
> So, what I suggest is if the init expression is a constant expression 
> (something we know at compile time) then the declarator is real 
> constexpr (no need for a new keyword)
> 
> For instance:
> 
> const int a = 1;
> const int b = a+1;
> 
> 
> Even if not constant, the compiler can do at compile time.
> for instance.
> int a = 1+2;
> So this has to be done anyway.
> The only difference is that using 'a' cannot be used as constant in 
> another expression.
> 
> 

So it looks like const/constexpr do different things, for example:


   const int a = rand();              // OK
   constexpr int b = rand();          // error

How about this:

   static int x;
   const int* p = &x;                // OK
   constexpr int* q = &x;            // ??

q's value isn't known at compile-time.