存储在 class 中的闭包中通过引用捕获的临时对象的生命周期

Lifetime of a temporary captured by reference in a closure stored in a class

考虑以下代码片段:

struct foo { };

template <typename F>
struct impl : F
{
    impl(F&& f) : F{std::move(f)} { }
    auto get() { return (*this)(); }
};

template <typename X>
auto returner(X&& x)
{
    return impl{[&x]{ return x; }};
//               ^~
}

int main()
{
    auto x = returner(foo{}).get();
}

live example on wandbox.org



标准在[class.temporary]中说:

Temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created.

[intro.execution]

A full-expression is

  • an unevaluated operand,

  • a constant-expression,

  • an init-declarator or a mem-initializer, including the constituent expressions of the initializer,

  • an invocation of a destructor generated at the end of the lifetime of an object other than a temporary object ([class.temporary]), or

  • an expression that is not a subexpression of another expression and that is not otherwise part of a full-expression.

我不确定与 foo{} 相关的 完整表达式 returner(foo{}) 还是 returner(foo{}).get().

这里的重要部分是:

A full-expression is [...] an expression that is not a subexpression of another expression and that is not otherwise part of a full-expression.

所以在returner(foo{}).get()中,returner(foo{})是表达式returner(foo{}).get()的一个子表达式,所以它不是一个完整的表达式。因此:

Is it guaranteed that foo{} will be alive for the entire duration of the returner(foo{}).get() expression?

是的。