c++ - 这是 C++11 for 循环的已知缺陷吗?

标签 c++ for-loop c++11 language-lawyer foreach

假设我们有一个结构,用于保存 3 个带有一些成员函数的 double :

struct Vector {
  double x, y, z;
  // ...
  Vector &negate() {
    x = -x; y = -y; z = -z;
    return *this;
  }
  Vector &normalize() {
     double s = 1./sqrt(x*x+y*y+z*z);
     x *= s; y *= s; z *= s;
     return *this;
  }
  // ...
};

为了简单起见,这有点做作,但我相信你同意类似的代码已经存在。这些方法允许您方便地链接,例如:

Vector v = ...;
v.normalize().negate();

甚至:

Vector v = Vector{1., 2., 3.}.normalize().negate();

现在,如果我们提供了 begin() 和 end() 函数,我们可以在新样式的 for 循环中使用我们的 Vector,比如循环 3 个坐标 x、y 和 z(毫无疑问,您可以构造更多"有用的”示例,将 Vector 替换为例如字符串):

Vector v = ...;
for (double x : v) { ... }

我们甚至可以这样做:

Vector v = ...;
for (double x : v.normalize().negate()) { ... }

还有:

for (double x : Vector{1., 2., 3.}) { ... }

但是,以下(在我看来)被破坏了:

for (double x : Vector{1., 2., 3.}.normalize()) { ... }

虽然这似乎是前两种用法的逻辑组合,但我认为最后一种用法会创建一个悬空引用,而前两种完全没问题。

  • 这是正确的并受到广泛赞赏吗?
  • 以上哪一部分是“坏”部分,应该避免?
  • 是否可以通过更改基于范围的 for 循环的定义来改进语言,以便在 for 表达式中构造的临时对象在循环期间存在?

最佳答案

Is this correct and Widely appreciated?

是的,你对事物的理解是正确的。

Which part of the above is the "bad" part, that should be avoided?

不好的部分是对从函数返回的临时值进行左值引用,并将其绑定(bind)到右值引用。就像这样糟糕:

auto &&t = Vector{1., 2., 3.}.normalize();

临时 Vector{1., 2., 3.} 的生命周期无法延长,因为编译器不知道 normalize 的返回值引用了它.

Would the language be improved by changing the definition of the range-based for loop such that temporaries constructed in the for-expression exist for the duration of the loop?

这与 C++ 的工作方式高度不一致。

它会防止人们在临时变量上使用链式表达式或对表达式使用各种惰性求值方法而造成某些问题吗?是的。但它也需要特殊情况的编译器代码,并且会混淆为什么它不能与 other 表达式构造一起使用。

更合理的解决方案是通知编译器函数的返回值始终是对 this 的引用,因此如果返回值绑定(bind)到临时扩展构造,那么它将扩展正确的临时。不过,这是一个语言级别的解决方案。

目前(如果编译器支持),您可以使其 normalize 不能 被临时调用:

struct Vector {
  double x, y, z;
  // ...
  Vector &normalize() & {
     double s = 1./sqrt(x*x+y*y+z*z);
     x *= s; y *= s; z *= s;
     return *this;
  }
  Vector &normalize() && = delete;
};

这将导致 Vector{1., 2., 3.}.normalize() 给出编译错误,而 v.normalize() 可以正常工作.显然,您将无法像这样做正确的事情:

Vector t = Vector{1., 2., 3.}.normalize();

但你也不能做错事。

或者,正如评论中所建议的,您可以使右值引用版本返回一个值而不是一个引用:

struct Vector {
  double x, y, z;
  // ...
  Vector &normalize() & {
     double s = 1./sqrt(x*x+y*y+z*z);
     x *= s; y *= s; z *= s;
     return *this;
  }
  Vector normalize() && {
     Vector ret = *this;
     ret.normalize();
     return ret;
  }
};

如果 Vector 是一种需要移动实际资源的类型,则可以使用 Vector ret = std::move(*this); 代替。命名的返回值优化使得这在性能方面达到了合理的最优。

关于c++ - 这是 C++11 for 循环的已知缺陷吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10593686/

相关文章:

c++ - 有符号字节和奇偶校验字节的区别

c++ - 使用模板实现排序。如何使用比较器?

javascript - 为什么将代码包装在resolved prop 中会使同步代码表现得像异步代码?

c++ - 在字符串中查找所有具有最大长度的有序序列

c++ - 使用 operator[] 访问 unique_ptr 的私有(private) std::map

c++ - 将 ptr_vector 与 malloc 一起使用

c - C 中的 For 循环出现奇怪的错误

bash - 使用两个参数循环文件 bash 脚本

c++ - 有没有办法在不定义模板函数的情况下为不同的 std::array 定义函数?

c++ - 具有数组或 vector 参数的类模板