Need explanation for the following expression in c++

huangapple go评论72阅读模式
英文:

Need explanation for the following expression in c++

问题

    static constexpr char const* const s_empty = "";

更像是一个验证性问题。据我理解这里
constexpr 会使 s_empty 指定符宣称可以在编译时评估变量的值。这意味着编译器可以用 const 折叠来替换它。

char/cost* 会使指针的赋值变得不可行,而最后的 const 会使引用的变量不可更改。

我希望这是正确的。

英文:
    static constexpr char const* const s_empty = "";

More like a validation question. As per my understanding here
constexpr would make s_empty specifier declares that it is possible to evaluate the value of the variable at compile time. And that would mean that the compiler can replace it for const folding.

char/cost* would make assignment to the pointer not feasible and the const in the end would make the referenced variable unchangeable.

I hope that is correct

答案1

得分: 1

Sure, here are the translated parts:

constexpr would make s_empty specifier declares that it is possible to evaluate the value of the variable at compile time

constexpr 会使 s_empty 声明成可以在编译时计算变量的值。

And that would mean that the compiler can replace it for const folding.

这意味着编译器可以用 const 折叠来替代它。

char/cost* would make assignment to the pointer not feasible

char/cost* 会使指针的赋值变得不可行。

and the const in the end would make the referenced variable unchangeable.

末尾的 const 会使引用的变量不可更改,但这是多余的,因为constexpr已经隐含了顶层const。

英文:

> constexpr would make s_empty specifier declares that it is possible to evaluate the value of the variable at compile time

constexpr enforces that the variable is initialized by a constant expression and makes the variable usable as part of other constant expressions. Constant expressions can be evaluated at compile-time and essentially a compiler has to do the evaluation of the variable's value at compile-time if marked constexpr (since it needs to diagnose whether the initialization is a constant expression).

For example, if you need to calculate the extent of an array variable and the calculation requires use of s_empty, then s_empty (likely) needs to be constexpr in order to make the calculation a constant expression. Extent of array variables are one of the contexts that require constant expressions.

> And that would mean that the compiler can replace it for const folding.

The compiler doesn't need to respect constexpr to determine whether or not constant folding is possible. Under the as-if rule it can always constant fold (or do any other optimization) as long as the observable behavior of the program doesn't change.

It may however adjust its internal heuristics with constexpr, since constexpr essentially forces calculation of the value anyway. However, for non-local static storage duration variables, the compiler has to check whether the initialization is a constant expression anyway, regardless of constexpr, because such variables may not have dynamic initialization (at runtime) if they are initialized by constant expressions.

> char/cost* would make assignment to the pointer not feasible

const on the left-hand side applies to char, not the pointer. It makes it so that the value that the pointer points to can't be modified. That's expected since you point it to a string literal, which are not allowed to be modified (and are already themselves const).

> and the const in the end would make the referenced variable unchangeable.

The const at the end makes the pointer value non-modifiable, but is completely redundant, because constexpr already implies a top-level const.

huangapple
  • 本文由 发表于 2023年5月13日 23:02:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定