【问题标题】:What exactly std::function() does when stored in a container?当存储在容器中时,std::function() 究竟做了什么?
【发布时间】:2020-10-27 01:48:16
【问题描述】:

std::function 构造函数在存储在容器中时究竟做了什么?

在此测试代码中:

struct A {
    int sn;
    A() = delete;
    A(int v) : sn(v) { cout << "A::init(" << sn << ')' << endl; }
    A(const A& a) : sn(a.sn+1) { cout << "A::copy(" << sn << ')' << endl; }
    A(A&& a) : sn(a.sn+1) { cout << "A::move(" << sn << ')' << endl; }
    ~A() { cout << "A::delete(" << sn << ')' << endl; }
};

void func(int a, A &b) {
    cout << "func2:" << a << ',' << b.sn << endl;
}

int main(int argc, char *argv[]) {
    std::vector<std::function<void(void)>> fv;
    A a(1);
    cout << "call bind()" << endl;
    fv.emplace_back(std::bind(func,1,a));
    fv.front()();
    cout << "end of local scope" << endl;
}

我声明class A 的移动和复制构造函数来累积一个序列号,这样我就可以将它作为第N 个创建的实例来跟踪。结果是:

A::init(1)
call bind()
A::copy(2)
A::move(3)
A::move(4)
A::delete(3)
A::delete(2)
func2:1,4
end of local scope
A::delete(1)
A::delete(4)

首先在main() 本地范围内创建实例a(1),然后在调用std::bind() 时将其复制为新实例a(2),然后再次移动到std::function 动态分配的内存(或其内部内存片段)中?) 保存std::bind() 实例的副本,所以我们有A 的第三个实例由std::function 实例保存。

目前为止都可以理解,但是为什么还有另一个移动构造呢?以及为什么第三个实例在第二个实例之前被销毁,应该在std::bind()返回时清除?

如果我将main() 函数重写为:

int main(int argc, char *argv[]) {
    A a(1);
    cout << "call bind()" << endl;
    std::function<void(void)> f(std::bind(func,1,a));
    f();
    cout << "end of local scope" << endl;
}

那么结果将是:

A::init(1)
call bind()
A::copy(2)
A::move(3)
A::delete(2)
func2:1,3
end of local scope
A::delete(3)
A::delete(1)

没有第二步构造,一切看起来都很合理。

在这种情况下调用emplace_back() 创建std::function 实例时究竟发生了什么?

【问题讨论】:

  • 不是向量,也不是std::function。这是std::bind。它的语义主要是实现定义的,并且它的参数可能会在他们安顿下来之前被玩弄一下...... ,在您的调试器中,通过这里发生的所有事情,并确定每次移动或复制的原因和原因。
  • 如果您将emplace_back 更改为push_back,您可能会觉得更奇怪。在我的快速测试中,第二步被省略了,在这种情况下,push_back 看起来比emplace_back 更有效率。
  • @SamVarshavchik 不,是std::function/emplace_back。更具体地说,它是完美的转发,表明它实际上并不完美。

标签: c++ std-function stdbind


【解决方案1】:

首先在main() 本地范围内创建一个实例a(1),然后在调用std::bind() 时将其复制为一个新实例a(2),然后再次移动到std::function 动态分配的内存中

您缺少一步:std::function 的构造函数采用按值方式

template<typename F>
std::function<void(void)>::function(F f);

emplace_back 正在转发包含a(2) 作为右值的std::bind-wrapper,是的,但是这个构造函数只接受值,而不是右值引用,所以a(2) 被移动并变成a(3)初始化构造函数的参数。 然后构造函数将该值移出到动态存储中。

int main() {
    A a(1); // A::init(1)
    // this is the temporary that gets materialized in main's scope for the call to emplace_back
    auto wrapper = std::bind(func, 1, a); // A::copy(2);
    // emplace_back receives (a reference to) the temporary as an rvalue, and so it passes std::function's constructor a reference to the same object, also as an rvalue
    // but std::function's constructor only takes values...
    std::function<void(void)> f(std::move(wrapper));
    // initializing the parameter of constructor (in main/emplace_back's context): A::move(3)
    // constructor then initializes dynamically allocated object: A::move(4)
    // constructor destroys parameter object: A::delete(3)
    // f gets destroyed, takes dynamically allocated object with it: A::delete(4)
    // wrapper (the temporary) gets destroyed (originally at the ; after emplace_back): A::delete(2)
    // a gets destroyed: A::delete(1);
}
// Note: the reason A::delete(4) comes before A::delete(1) in my version but not yours is that your A(4) lives inside fv, which is created at the top of main and thus is the last thing destroyed, but mine lives inside f, which is created at the end and is the first thing destroyed

您简化的不同之处在于您没有将std::bind 的结果作为临时实现。基本上,emplace_back 要求参数已经完全构造(就像任何函数调用一样)。因此,您必须执行std::bind(以及其中的副本)才能调用emplace_back,然后然后 emplace_back 将该对象移动到构造函数参数。但是,当您将std::bind prvalue 直接传递给std::function 构造函数时,构造函数参数对象是通过在执行过程中将std::bind 的结果对象设置为参数对象来直接构造的。

另一种说法:std::function 的构造函数必须用新创建的对象调用。使用emplace_back 时,中间函数“忘记”std::bind 调用是一个新构造的对象,因此通过执行“无关”移动来构造一个新对象。当您删除emplace_back 时,不会忘记std::bind 对象是新构造的这一事实,并且会忽略移动。

另一种说法:emplace_back 声称的“完美”转发实际上并不完美;这样的事情是不可能的(必须修改用户代码)。 “完美”转发将左值作为左值转发,但将 xvalues 和 prvalues 作为 xvalues 折叠(通过要求在调用函数之前实现 prvalues)。使用 xvalue 调用 std::function 的构造函数会导致两次移动,但使用 prvalue 调用会导致一次。您的第二个版本将纯右值传递给构造函数,但原始版本传递了一个 xvalue,因此它们具有不同的行为。

您可以使用包装函子获得所需的结果:

template<typename F>
struct initializer {
    F f;
    operator decltype(f())() && {
        return std::move(f)();
    }
};
int main(int argc, char *argv[]) {
    std::vector<std::function<void(void)>> fv;
    A a(1);
    cout << "call bind()" << endl;
    fv.emplace_back(
        initializer{[&]() -> std::function<void(void)> {
            return std::bind(func, 1, a);
        }});
    fv.front()();
    cout << "end of local scope" << endl;
}

这会将对std::bind的调用直接置于对std::function的构造函数的调用之下(在return中),因此prvalue直接初始化构造函数参数而没有干预移动。 std::function 也直接在向量中的“空白”空间中构造,无需进一步移动(毕竟emplace_back 就是这样做的)。

【讨论】:

  • OK,所以 emplace_back 会强制从std::bind()返回的右值创建一个新实例,然后std::move()那个新实例到std::function构造函数,只是因为构造函数只接受传值,不是引用,对吗?如果我改用push_back(),将首先使用临时A::move(3) 创建一个新的std::function(),然后使用std::function(function &amp;&amp;) 移动构造函数在std::vector 的动态内存中创建另一个std::function(),所以我不会见 A::move(4) 但仍有移动构造。
猜你喜欢
  • 2011-06-18
  • 2012-07-23
  • 2016-09-10
  • 2023-03-15
  • 2012-10-17
  • 2021-06-04
  • 1970-01-01
  • 2018-07-30
  • 2019-10-06
相关资源
最近更新 更多