我有这个代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
目的是通过reference_wrapper传递functor对象,以避免无用的复制costructor调用.我期待以下输出:
F: 6
7
Run Code Online (Sandbox Code Playgroud)
它适用于GCC> = 4.4.0,Visual Studio 2008,并通过用boost替换std :: tr1命名空间来提升.它仅适用于新的Visual Studio 2010 Express Beta 2和Release Candidate.
这个新的C++功能在vs2010中有漏洞吗?或者代码中有一些错误或误用?
我已经写了一些代码进行计数用算符和矢量的元素的数量ref和bind模板从boost::或std::(对于C++ 11)的命名空间.我正在使用a #define来切换boost::和std::名称空间.我正在使用boost版本1.53,我的编译命令是g++ test.cpp -std=c++11.我已经尝试过使用gcc版本4.7.2和4.6.3,但两者都有相同的错误.
我有3个问题:
std和boost版本bind,ref和function?(我看到了这个问题,但答案没有提及ref或function)谢谢!
PS这个例子只是说明了我的问题,我知道size()了std::vector:-)
//#define USE_STD
#ifdef USE_STD
#include <functional>
using namespace std::placeholders;
namespace impl = std;
#else
#include <boost/version.hpp>
#include <boost/bind.hpp>
#include <boost/ref.hpp>
namespace impl = boost;
#endif
#include <iostream>
#include <algorithm>
#include <vector>
class Item { …Run Code Online (Sandbox Code Playgroud)