C++11 §5.1.2/6
The closure type for a lambda-expression with no lambda-capture has a [...] conversion function [...]. The value returned by this conversion function shall be the address of a function that, when invoked, has the same effect as invoking the closure type’s function call operator.
注意:这并不是说转换将始终为同一类型返回相同 值。那么,下面的程序是否与 UB 和它所暗示的所有意义破坏相冲突?
int main() {
auto f = []{};
return ((+f) == (+f));
}
注意:如果任何真正的编译器给出 true
以外的任何东西,我会非常惊讶,但问题是编译器是否可以合法做任何其他事情?
编辑:
C++11 §1.3.24
undefined behavior
behavior for which this International Standard imposes no requirements [ Note: Undefined behavior may be expected when this International Standard omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data
最佳答案
不,它既不是未定义也不是未指定。您无法提前知道结果是否总是相同(true
或false
),但其中没有任何内容意味着您的程序具有未定义的行为或结果属于“未指定值”的定义。
简单地说,这与调用 rand()
或什至 &someVariable
的 UB 差不多。
关于c++ - 此 c++11 lambda 代码是否调用未定义的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24215683/