7

在使用 ranged-for 循环时,我得到了悬空引用。考虑以下 C++14 表达式(下面的完整示例程序):

    for(auto& wheel: Bike().wheels_reference())
        wheel.inflate();

它的输出是:

 Wheel()
 Wheel()
 Bike()
~Bike() with 0 inflated wheels.
~Wheel()
~Wheel()
 Wheel::inflate()
 Wheel::inflate()

显然有些事情出了问题。轮子在其生命周期之外被访问,结果为 0,而不是预期的 2。

一个简单的解决方法是为Bikein引入一个变量main。但是,我不控制mainor中的代码Wheel。我只能更改 struct Bike

有没有办法只通过改变来修复这个例子Bike

一个成功的解决方案要么在编译时失败,要么计算 2 个充气轮胎并且在其生命周期之外不接触任何对象。

附录:编译准备好的源码

#include <cstdlib>
#include <iostream>
#include <array>
#include <algorithm>
using std::cout;
using std::endl;

struct Wheel
{
    Wheel() { cout << " Wheel()" << endl; }
    ~Wheel() { cout << "~Wheel()" << endl; }
    void inflate() { inflated = true; cout << " Wheel::inflate()" << endl; }
    bool inflated = false;
};

struct Bike
{
    Bike() { cout << " Bike()" << endl; }
    ~Bike() {
        cout << "~Bike() with " << std::count_if(wheels.begin(),    wheels.end(),
            [](auto& w) { return w.inflated; }) << " inflated wheels." << endl;
    }
    std::array<Wheel, 2>& wheels_reference() { return wheels; }
    std::array<Wheel, 2> wheels{Wheel(), Wheel()};
};

int main()
{
    for(auto& wheel: Bike().wheels_reference())
        wheel.inflate();
    return EXIT_SUCCESS;
}
4

4 回答 4

6

删除 的右值重载wheels_reference

std::array<Wheel, 2>& wheels_reference() & { return wheels; }
std::array<Wheel, 2>& wheels_reference() && = delete;

这样您就不会返回对临时成员的引用。

Bike您对该类的示例用法

for(auto& wheel: Bike().wheels_reference())
    wheel.inflate();

然后将拒绝编译(clang 3.4 输出):

test.cpp:31:29: error: call to deleted member function 'wheels_reference'
    for(auto& wheel: Bike().wheels_reference())
                     ~~~~~~~^~~~~~~~~~~~~~~~
test.cpp:24:27: note: candidate function has been explicitly deleted
    std::array<Wheel, 2>& wheels_reference() && = delete;
                          ^
test.cpp:23:27: note: candidate function not viable: no known conversion from 'Bike' to 'Bike' for object argument
    std::array<Wheel, 2>& wheels_reference() & { return wheels; }

如果临时的生命周期是手动延长的事情工作。

Bike&& bike = Bike();
for(auto& wheel: bike.wheels_reference())
    wheel.inflate();
于 2016-01-14T15:02:56.443 回答
3

最好的解决方案是通过临时调用成员函数来停止获取类型的成员。

如果wheels_reference是一个非成员函数,你可以简单地声明它是这样的:

wheels_reference(Bike &bike);

由于非 const 左值参数不能附加到临时参数,您将无法调用wheels_reference(Bike()). 由于wheels_reference是一个成员函数,你只需要使用成员函数语法来表达同样的事情:

std::array<Wheel, 2>& wheels_reference() & //<--
{ return wheels; }

如果用户现在尝试调用Bike().wheels_reference(),编译器会抱怨。

于 2016-01-14T15:07:01.790 回答
1

以下可怕的装置似乎满足所有条件:

#include <memory>

struct Bike
{
    // Bike() { cout << " FakeBike()" << endl; }
    // ~Bike() { cout << "~FakeBike()" << endl; }
    struct RealBike;
    struct Wrap {
        std::shared_ptr<RealBike> parent;
        auto begin() { return parent->wheels.begin(); }
        auto end() { return parent->wheels.end(); }
    };
    struct RealBike {
        RealBike() { cout << " Bike()" << endl; }
        ~RealBike() {
            cout << "~Bike() with " << std::count_if(wheels.begin(),    wheels.end(),
                [](auto& w) { return w.inflated; }) << " inflated wheels." << endl;
        }
        std::array<Wheel, 2> wheels;
    };
    std::shared_ptr<RealBike> real = std::make_shared<RealBike>();
    Wrap wheels_reference() { return Wrap{real}; }
};

我不喜欢的是它需要包装std::array<Wheel, 2>in的所有 API Wrap

于 2016-01-14T14:47:01.933 回答
1

您可以只添加几个 cv-ref 限定的wheels_reference成员函数重载:

std::array<Wheel, 2>& wheels_reference() & { return wheels; }

std::array<Wheel, 2> const & wheels_reference() const & { return wheels; }

std::array<Wheel, 2> wheels_reference() && { return std::move(wheels); }

std::array<Wheel, 2> wheels_reference() const && { return wheels; }

请注意,如果对象是临时的(&&案例),那么您应该返回一个值(从数据成员复制构造,或者更好地从它移动构造),也不应该引用。

拥有所有四个重载,您可以覆盖所有可能的用例。

于 2016-01-15T14:20:53.463 回答