c++ - 将函数标识符作为右值引用传递并对其应用 std::move()
问题描述
考虑以下代码段
#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
这里是多余的。
推荐阅读
- racket - 我可以在球拍中制作操作系统级别的键盘绑定吗?
- javascript - 如何添加
- 元素与元素
- 点击java脚本后
- 元素与元素
- mysql - 如何按字母顺序重新排序表中的列数据值
- java - Java Hibernate java.lang.IllegalArgumentException:参数值与预期类型不匹配
- google-app-engine - 使用 Java API 从 Google Sheet 创建 BigQuery 表 - 访问被拒绝
- r - R中的组合没有重复
- html - HTML 中的 MDL 卡片定位
- csv - 如何使用我们在 pandas 中使用的 error_bad_line = False 在 pyspark 中读取 csv
- ios - GStreamer-无法构建管道:没有元素 tcppclientsrc
- visual-studio-code - 禁用不平衡括号