Std :: for_each for a member function with 1 argument

I am wondering how to implement what is indicated in the title. I tried something like ...

std::for_each( a.begin(), a.end(), std::mem_fun_ref( &myClass::someFunc ) )

but I get a message that the "term" (I assume that this means the third argument) does not evaluate a function with 1 argument, although it someFunctakes one argument - the type of objects stored in a.

I am wondering if it is possible that I am trying to do using the standard library (I know that I can do this easily using boost).

PS Is there any use for_eachand mem_fun_refperformance implications compared to simply doing it amanually and passing the object in someFunc?

+3
source share
2 answers

Although someFunc is a member with one parameter, mem_fun_ref uses the implicit first argument to "myClass". You want to use vector elements as the second argument.

And there are probably no negative consequences for using for_each and mem_fun_ref. The compiler will generate comparable code. But, the only way to be sure of control :)

  std::for_each(a.begin(), a.end(),
                std::bind1st(
                    std::mem_fun_ref( &MyClass::SomeFunc ),
                    my_class ));
+2
source

I think you need to use bind_1st to provide the hidden argument to "this". Or do you mean that the argument "this" is the only one, someFunc does not have its own parameters?

+1
source

Source: https://habr.com/ru/post/1739732/


All Articles