基于范围的临时对象

Mor*_*kus 18 c++ qt c++11

我知道一般来说,基于范围的for循环中临时的生命周期延伸到整个循环(我读过C++ 11:基于范围的语句:"range-init"生命周期?).所以做这样的事情一般都可以:

for (auto &thingy : func_that_returns_eg_a_vector())
  std::cout << thingy;
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试做一些我认为与Qt QList容器类似的事情时,我对内存问题感到磕磕绊:

#include <iostream>
#include <QList>

int main() {
  for (auto i : QList<int>{} << 1 << 2 << 3)
    std::cout << i << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这里的问题是valgrind在QList类中的某个地方显示无效的内存访问.但是,修改示例以便将列表存储在变量中可以提供正确的结果:

#include <iostream>
#include <QList>

int main() {
  auto things = QList<int>{} << 1 << 2 << 3;
  for (auto i : things)
    std::cout << i << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在我的问题是:我在第一种情况下做了一些愚蠢的事情,导致例如未定义的行为(我没有足够的经验阅读C++标准以便为自己回答这个问题)?或者这是我如何使用QList或如何QList实施的问题?

ken*_*ytm 12

由于您使用的是C++ 11,因此可以使用初始化列表.这将通过valgrind:

int main() {
  for (auto i : QList<int>{1, 2, 3})
    std::cout << i << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题与基于范围的甚至C++ 11并不完全相关.以下代码演示了同样的问题:

QList<int>& things = QList<int>() << 1;
things.end();
Run Code Online (Sandbox Code Playgroud)

要么:

#include <iostream>

struct S {
    int* x;

    S() { x = NULL; }
    ~S() { delete x; }

    S& foo(int y) {
        x = new int(y);
        return *this;
    }
};

int main() {
    S& things = S().foo(2);
    std::cout << *things.x << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

无效读取是因为表达式S()(或QList<int>{})中的临时对象在声明之后被破坏(在C++ 03和C++11§12.2/ 5之后),因为编译器不知道方法foo()(或operator<<)将返回那个临时对象.所以你现在正在引用释放内存的内容.


cvo*_*scu 7

编译器不可能知道作为三次调用的结果的引用operator <<绑定到临时对象QList<int>{},因此临时的生命期不会延长.编译器不知道(并且不能期望知道)关于函数返回值的任何信息,除了它的类型.如果它是一个引用,它不知道它可能绑定到什么.我很确定,为了适用延长寿命的规则,绑定必须是直接的.

这应该有效,因为列表不再是临时的:

#include <iostream>
#include <QList>

int main() {
  auto things = QList<int>{};
  for (auto i : things << 1 << 2 << 3)
    std::cout << i << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这应该有效,因为绑定是直接的,因此规则可以适用:

#include <iostream>
#include <QList>

int main() {
  for (auto i : QList<int>{1, 2, 3})
    std::cout << i << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)