我看过类似以下用法的东西:
#include <iostream>
#include <functional>
using namespace std;
template<typename FN>
void Foo(FN&& Fn)
{
Fn();
}
void b()
{
cout << "2." << endl;
}
int main()
{
Foo([](){ cout << "1." << endl; });
Foo(&b);
auto c = []() { cout << "3." << endl; };
Foo(c);
std::function<void(void)> d = c;
Foo(d);
return 0;
}
我相当确定 'c' 是一个左值,但我可以相信有一些 lambda 类型推导的诡计。但我几乎百分之百确定,d 是一个左值。
如果函数接受一个右值,那么模板化的内容为什么会起作用,但是 d 是一个左值呢?
此外,为什么有人会这样写 Foo 的签名,而不只是:
template<typename FN>
void Foo(FN Fn)
std::forward()
)。“通用引用”是Scott Meyers使用的术语。 - Barry