How to use std :: ref?

What is the correct way to use std::ref ? I tried the following code in VS2010 and it does not 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': the character 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 ';' to '<'

Plus more ...

+9
c ++ c ++ 11 stl
Mar 06 2018-12-12T00:
source share
2 answers

There is an error or set of errors in the implementation of Visual C ++ 10.0 std::ref .

It is reported that it has been fixed for Visual C ++ 11; see my previous question.

Microsoft's STL replied like this: “We have already fixed it, and the fix will be available in RTM VC11 (however, the fix did not make it into the beta version of VC11.)”

+8
Mar 06 '12 at 11:17
source share

I got the same compilation error with VS2010 and fixed it, inheriting from std::unary_function :

 struct IsEven : std::unary_function<int, bool> 

I only considered this because of the result appearing in the error message. I can only guess that std::ref , in VS2010, depends on typedef in unary_function :

 template <class Arg, class Result> struct unary_function { typedef Arg argument_type; typedef Result result_type; }; 

EDIT:

See Cheers and hth's answer. - Alf regarding bug in VS2010.

+4
Mar 06 '12 at 11:13
source share



All Articles