如何使用std :: ref? [英] How to use std::ref?

查看:114
本文介绍了如何使用std :: ref?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 std :: ref 的正确方法是什么?我试着在VS2010下面的代码,它不编译:

  #include< vector& 
#include< algorithm>
#include< iostream>
#include< functional>
using namespace std;
struct IsEven
{
bool operator()(int n)
{
if(n%2 == 0)
{
evens.push_back(n);
return false;
}

return true;
}

vector< int>埃文斯
};
int main(int argc,char ** argv)
{
vector< int> v;
for(int i = 0; i <10; ++ i)
{
v.push_back(i);
}

IsEven f;
vector< int> :: iterator newEnd = remove_if(v.begin(),v.end(),std :: ref(f));
return 0;
}

错误:



< >

c:\program files(x86)\microsoft visual studio
10.0\vc\include\xxresult(28):error C2903:'result':symbol is neither类模板或函数模板



c:\program files(x86)\microsoft visual studio
10.0 \vc\include\xxresult (28):error C2143:syntax error:missing';'before'<'


p>

解决方案

Visual C ++ 10.0中有一个或一组错误 std :: ref



它已被修正为Visual C ++ 11;请参阅我的有关此问题的早期问题

$微软的STL回答说:我们已经解决了这个问题,并且修复程序将在VC11 RTM中提供(但是,修复没有进入VC11测试版。)


What is the correct way to use std::ref? I tried following code in VS2010 and it doesn't compile:

#include <vector>
#include <algorithm>
#include <iostream>
#include <functional>
using namespace std;
struct IsEven
{
    bool operator()(int n) 
    {
        if(n % 2 == 0)
        {
            evens.push_back(n);
            return false;
        }

        return true;
    }

    vector<int> evens;
};
int main(int argc, char **argv)
{
    vector<int> v;
    for(int i = 0; i < 10; ++i)
    {
        v.push_back(i);
    }

    IsEven f;
    vector<int>::iterator newEnd = remove_if(v.begin(), v.end(), std::ref(f));
    return 0;
}

Errors:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxresult(28): error C2903: 'result' : symbol is neither a class template nor a function template

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxresult(28): error C2143: syntax error : missing ';' before '<'

Plus some more...

解决方案

There is a bug or set of bugs in Visual C++ 10.0 implementation of std::ref.

It has reportedly been fixed for Visual C++ 11; see my earlier question about it.

STL at Microsoft answered thusly: "We've already fixed it, and the fix will be available in VC11 RTM. (However, the fix didn't get into the VC11 Beta.)"

这篇关于如何使用std :: ref?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆