为什么从lambda返回const引用会导致临时引用?

时间:2023-01-22 10:50:17

I have a situation where I have a member returning a const&, and then this result is being forwarded within a lambda, which has the same return type.

我有这样一种情况,我有一个成员返回一个const&,然后这个结果在一个lambda中被转发,它具有相同的返回类型。

MSVC2017 identifies this situation as risky, and issues a warning: returning address of local variable or temporary. Empirical testing with clang and other compilers shows this is true across the board. What I do not understand, is why this is different than several method calls which all return the same type.

MSVC2017认为这种情况存在风险,并发出警告:返回本地变量或临时地址。用clang和其他编译器进行的经验测试表明,这是全面的。我不明白的是,为什么这与几个返回相同类型的方法调用不同。

For example, this works perfectly:

例如,这个方法非常有效:

class A {
public:
    const std::string& name() const { return m_name; }
private:
    std::string m_name;
};

class B {
public:
    const std::string& name() const { return m_a.name(); }
private:
    A m_a;
};

//...
B b;
std::cout << b.name();

Works as expected, no warnings/errors at compile or runtime.

按预期工作,在编译或运行时没有警告/错误。

But with a lambda, it doesn't:

但是对于一个lambda,它并没有:

class A {
public:
    const std::string& name() const { return m_name; }
private:
    std::string m_name;
};

//...
using Getter = std::function< const std::string&() >;
A a;
Getter g = [&a] { return a.name(); };
std::cout << g();

results in a crash, or at least printing corrupted memory

导致崩溃,或至少打印损坏的内存

Can someone point me to some info about why this does not work? I would generally expect it to work the same...

有人能告诉我为什么它不能工作吗?一般来说,我认为它的工作原理是一样的……

1 个解决方案

#1


11  

The return type of your lambda is not a reference. This is the cause of all your problems.

您的lambda的返回类型不是引用。这是你所有问题的根源。

Your lambda returns a copy of the name. You are storing this lambda in a std::function returning a const std::string&, which means that effectively, you will return a reference to that copy which will get destroyed as soon as std::function's call operator returns!1

您的lambda返回名称的副本。您正在将这个lambda存储在std::函数中,返回const std::string&,这意味着您将有效地返回对该副本的引用,该副本将在std::::函数的调用操作符返回时被销毁

Naturally, the fix is to change the return type of the lambda:

当然,修正是更改lambda的返回类型:

Getter g = [&a]() -> const std::string& { return a.name(); };

// or
Getter g = [&a]() -> auto& { return a.name(); };
// or if you are feeling fancy :P
Getter g = [&a]() -> decltype(auto) { return a.name(); };

1: To expand a bit on this, you can imagine std::function's implementation as something like this (only the relevant parts are shown and massively simplified):

1:为了扩展这一点,您可以想象std::函数的实现是这样的(只显示相关的部分,并大大简化):

template<typename R, typename... Ts>
struct function<R(Ts...)> {
  R operator()(Ts... Args) const {
    return callInternalFunctionObject(Args...); // here: copy will get destructed
  }
};
// R=const std::string&, and Ts is empty

#1


11  

The return type of your lambda is not a reference. This is the cause of all your problems.

您的lambda的返回类型不是引用。这是你所有问题的根源。

Your lambda returns a copy of the name. You are storing this lambda in a std::function returning a const std::string&, which means that effectively, you will return a reference to that copy which will get destroyed as soon as std::function's call operator returns!1

您的lambda返回名称的副本。您正在将这个lambda存储在std::函数中,返回const std::string&,这意味着您将有效地返回对该副本的引用,该副本将在std::::函数的调用操作符返回时被销毁

Naturally, the fix is to change the return type of the lambda:

当然,修正是更改lambda的返回类型:

Getter g = [&a]() -> const std::string& { return a.name(); };

// or
Getter g = [&a]() -> auto& { return a.name(); };
// or if you are feeling fancy :P
Getter g = [&a]() -> decltype(auto) { return a.name(); };

1: To expand a bit on this, you can imagine std::function's implementation as something like this (only the relevant parts are shown and massively simplified):

1:为了扩展这一点,您可以想象std::函数的实现是这样的(只显示相关的部分,并大大简化):

template<typename R, typename... Ts>
struct function<R(Ts...)> {
  R operator()(Ts... Args) const {
    return callInternalFunctionObject(Args...); // here: copy will get destructed
  }
};
// R=const std::string&, and Ts is empty