考虑以下代码段
#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
处于无效状态吗?