我有这个代码:
#include <iostream>
#include <functional>
struct A
{
int operator()(int i) const {
std::cout << "F: " << i << std::endl;
return i + 1;
}
};
int main()
{
A a;
std::tr1::function<int(int)> f = std::tr1::ref(a);
std::cout << f(6) << std::endl;
}
目的是通过 reference_wrapper 传递函子对象,以避免无用的复制 costructor 调用。我期望以下输出:
F: 6
7
它可以与 GCC >= 4.4.0、Visual Studio 2008 以及通过将 std::tr1 命名空间替换为 boost 来正常工作。它仅不适用于新的 Visual Studio 2010 Express Beta 2 和 Release Candidate。
这个新的 C++ 功能在 vs2010 中是否存在错误?还是代码中有一些错误或误用?