c++ - 此 c++11 lambda 代码是否调用未定义的行为?

标签 c++ c++11 lambda language-lawyer undefined-behavior

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

最佳答案

不,它既不是未定义也不是未指定。您无法提前知道结果是否总是相同(truefalse),但其中没有任何内容意味着您的程序具有未定义的行为或结果属于“未指定值”的定义。

简单地说,这与调用 rand() 或什至 &someVariable 的 UB 差不多。

关于c++ - 此 c++11 lambda 代码是否调用未定义的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24215683/

相关文章:

c++ - 为什么在这种情况下创建临时实例不起作用?

python - 如何在 python tkinter 中分离 View 和 Controller ?

python - Python 3 中的过滤对象错误

java - 使用 Java native 代码或 C++(QT、WxWidgets 等)进行独立于平台的编程

c++ - 使用 Makefile 构建时在 Xcode 中忽略的断点

c++ - 如何使用 std::transform 进行计算

c++ - 在参数构造中使用 std::move

java - Java 8 中对多维数组的方法引用

c++ - 使用 "using"引用特定项目是一种好习惯吗?

c++ - 使用 ssh 重新连接到远程 Ubuntu 机器后,如何继续使用 gdb 调试 c++ 代码?