相关疑难解决方法(0)

Visual Studio 2010和std :: function

我有这个代码:

#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中有漏洞吗?或者代码中有一些错误或误用?

c++ functional-programming visual-studio-2010 functor c++11

11
推荐指数
1
解决办法
2万
查看次数

对于boost :: ref没有匹配的调用错误,但是对于std :: ref没有

我已经写了一些代码进行计数用算符和矢量的元素的数量refbind模板从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个问题:

  1. 我不明白为例2生成的错误.
  2. 是否可以通过切换命名空间来使这样的代码成为可移植的?
  3. 是否有一个很好的参考,详细描述之间的差异stdboost版本bind,reffunction?(我看到了这个问题,但答案没有提及reffunction)

谢谢!

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)

c++ boost c++11 boost-ref

6
推荐指数
1
解决办法
876
查看次数