将函数标识符作为右值引用传递并对其应用 std::move()

Passing a function identifier as an rvalue reference and applying std::move() to it

考虑以下片段

#include <iostream>
#include <functional>

using callback = std::function<double (double, double)>;


double sum (double a, double b) {
    return a + b;
}


int main (int argc, char *argv[]) {
    // Shouldn't this leave sum() in an invalid state?
    auto c = std::move(sum);

    std::cout << c(4, 5) << std::endl;
    std::cout << sum(4, 5) << std::endl;

    return EXIT_SUCCESS;
}

我正在将 sum 转换为右值引用,将其存储在 c 中,并在没有明显错误行为的情况下调用这两个函数。这是为什么? std::move 不应该让 sum 处于无效状态吗?

您将指针移动到函数,而不是函数:

callback c = std::move(sum);

此处使用 std::move 是多余的。