c++ - transfer loop into C++03 for_each -
c++ - transfer loop into C++03 for_each -
possible duplicate: using bind1st method takes argument reference
i have next traditional c++03 loop (using auto
stack overflow space efficiency only):
for (auto = some_vector.begin(); != some_vector.end(); ++it) { foobar.method(*it); }
in c++11, managed rewrite next for_each
invocation works well:
std::for_each(some_vector.begin(), some_vector.end(), std::bind(&foobar::method, std::ref(foobar), _1));
(of course of study utilize lambda in c++11, that's not point.) unfortunately, std::bind
not part of c++03, tried simulating std::bind1st
, std::mem_fun_ref
:
std::for_each(some_vector.begin(), some_vector.end(), std::bind1st(std::mem_fun_ref(&foobar::method), std::ref(foobar)));
but triggered c2535 error in visual studio ("member function defined or declared"):
// within class binder1st in header xfunctional result_type operator()(const argument_type& _right) const { // apply functor operands homecoming (op(value, _right)); } result_type operator()(argument_type& _right) const { // apply functor operands <--- error c2535 here homecoming (op(value, _right)); }
is const-correctness bug in visual studio, or have done wrong?
also, std::ref
not seem part of c++03. there workaround?
why not utilize std::mem_fun
? expects pointer first parameter, such:
#include <functional> #include <algorithm> #include <vector> struct foo{ void method(int){} }; int main(){ std::vector<int> v; foo f; std::for_each(v.begin(), v.end(), std::bind1st(std::mem_fun(&foo::method), &f)); }
c++ visual-studio-2010 stl foreach c++11
Comments
Post a Comment