将引用(右值)移动到函数
Move reference (rvalue) to function
我正在阅读一些文档并看到以下内容:
template<class Ret, class... Args>
struct is_function<Ret(Args...) &&> : std::true_type {};
引自:http://en.cppreference.com/w/cpp/types/is_function
如何对函数进行右值引用?
据我了解,函数没有存储生命周期。有人可以解释一下吗?我理解引用和指针,但如何"移动"函数?
我写了这段代码,它编译并运行起来:
#include <iostream>
using namespace std;
int foo(int num) {
return num + 1;
}
int main() {
int (*bar1)(int) = &foo;
cout << bar1(1) << endl;
int (&bar2)(int) = foo;
cout << bar2(2) << endl;
auto bar3 = std::move(bar2); // ????
cout << bar3(3) << endl;
cout << bar2(2) << endl;
int (&&bar4)(int) = foo; // ????
cout << bar4(4) << endl;
}
假设您是否可以将函数作为字节码/操作码存储在内存中,并"移动"它。CPU 不会阻止它运行吗?
编辑:@NicolBolas纠正了我的误解,但这是我另一个"问题"的答案:函数的右值引用
如何对函数进行右值引用?
这不是这个意思。
Ret(Args...) &&
末尾的&&
是指成员函数具有右值this
的能力。因此,专用化适用于将Ret
作为返回值、Args
作为其参数并使用右值this
的函数类型。
所以它不是"对函数的右值引用"。这是一个采用右值this
的函数。
相关文章:
- 为什么不调用移动构造函数?(默认情况下只有构造器,没有别的)
- std::vector::p ush_back() 不会在 MSVC 上编译具有已删除移动构造函数的对象
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 为什么调用复制构造函数而不是移动构造函数?
- 移动赋值运算符;尝试引用已删除的函数.我该如何解决这个问题?
- 基类中的默认析构函数禁用子类中的移动构造函数(如果有成员)
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 具有已删除移动和复制构造函数的类的就地构造
- 使用移动和复制语义时函数匹配如何工作?
- 移动构造函数和右值引用
- 使用移动调用对等构造函数unique_ptr默认构造函数
- 为什么 std::memmove 中联合的默认非平凡移动构造函数C++?
- 使用宏来覆盖移动函数的接口的缺点
- 在头文件中移动函数以获得干净的排序
- 为什么派生类中复制/移动函数"normal"实现的行为会有所不同,具体取决于它们的定义方式?
- 在没有移动函数的情况下返回不可复制对象的解决方法
- 从技术上讲,如何在不复制的情况下移动函数返回值
- 我是否需要添加空测试无处不在,一旦我有移动函数
- 如果函数调用是返回语句,编译器可以自动移动函数参数吗?
- 当派生函数定义了析构函数时,使用复制函数而不是移动函数