更新时间:2023-12-01 08:51:16
来自 expr.const] :
表达式e是核心常量表达式,除非按照抽象机的规则对e的求值将对以下表达式之一求值:
An expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine, would evaluate one of the following expressions:
从左值到右值的转换,除非将其应用于
an lvalue-to-rvalue conversion unless it is applied to
[...]
在f(x)
中,我们在x
上执行从左值到右值的转换. x
不是整数或枚举类型,它不是字符串字面量的子对象,也不是用constexpr定义的对象,并且它的生命周期不是从f(x)
的评估开始的.
In f(x)
, we do an lvalue-to-rvalue conversion on x
. x
isn't of integral or enumeration type, it's not a subobject of a string-literal, it's not an object defined with constexpr, and its lifetime did not begin with the evaluation of f(x)
.
这似乎使它不是核心常量表达式.
That seems to make this not a core constant expression.
,由于S
为空,因此其隐式生成的副本构造函数中的任何内容实际上都不会触发此左值到右值转换.这意味着该表达式中的任何内容实际上都没有违反任何核心常量表达式限制,因此gcc和clang在接受它方面是正确的.这种解释对我来说似乎是正确的. constexpr
很有趣.
However, as Casey points out, since S
is empty, nothing in its implicitly-generated copy constructor would actually trigger this lvalue-to-rvalue conversion. That would mean that nothing in this expression actually violates any of the core constant expression restrictions, and hence gcc and clang are correct in accepting it. This interpretation seems correct to me. constexpr
is fun.