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.
|
|