How to bind class static member function?

F

flyaflya

i use a class static member function to for_each:
class t1
{
static test(string s);
}

main()
{
list<string> slist;
...
for_each(slist.begin(), slist.end(), t1::test);
}
this can run, but when i give t1::test a parameter,like this :
class t1
{
static test2(string s, int 5);
}
main()
{
for_each(slist.begin(), slist.end(), bind2nd(t1::test2,5) ); //error !!!
for_each(slist.begin(), slist.end(), bind2nd(ptr_fun(t1::test2,5) ) );
//also error !!!
}

how to resolve this problem?
 
V

Victor Bazarov

flyaflya said:
i use a class static member function to for_each:
class t1
{
static test(string s);
}

main()
{
list<string> slist;
...
for_each(slist.begin(), slist.end(), t1::test);
}
this can run, but when i give t1::test a parameter,like this :
class t1
{
static test2(string s, int 5);
}
main()
{
for_each(slist.begin(), slist.end(), bind2nd(t1::test2,5) );
//error !!! for_each(slist.begin(), slist.end(),
bind2nd(ptr_fun(t1::test2,5) ) ); //also error !!!

The closing parenthesis is in the wrong place.
}

how to resolve this problem?

Use 'bind2nd(ptr_fun(t1::test2),5)' as the third argument to 'for_each'.
-----------------------------
struct A {
static void foo(double,int);
};

#include <list>
#include <functional>
#include <algorithm>
#include <utility>

int main() {
std::list<double> d;
std::for_each(d.begin(), d.end(),
std::bind2nd(std::ptr_fun(A::foo), 5));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,919
Messages
2,570,037
Members
46,445
Latest member
ElizbethDe

Latest Threads

Top