Skip to content

Instantly share code, notes, and snippets.

@Offirmo
Last active September 25, 2017 23:57
Show Gist options
  • Save Offirmo/4713286 to your computer and use it in GitHub Desktop.
Save Offirmo/4713286 to your computer and use it in GitHub Desktop.
[Some C++] #c++
#include <iostream>
#include <functional>
class MemFunTest
{
public:
bool test0()
{
std::cout << "[" << this <<"-test0 called]" << std::endl;
return false;
}
bool test1(int a)
{
std::cout << "[" << this <<"-test1(" << a << ") called]" << std::endl;
return false;
}
bool test2(int a, int b)
{
std::cout << "[" << this <<"-test2(" << a << "," << b << ") called]" << std::endl;
return false;
}
};
int main()
{
// for member function, use std::mem_fun and std::bind1st
std::mem_fun_t< bool, MemFunTest> fo0 = std::mem_fun(&MemFunTest::test0);
std::mem_fun1_t<bool, MemFunTest, int> fo1 = std::mem_fun(&MemFunTest::test1);
//std::mem_fun2_t<bool, MemFunTest, int, int> fo2 = std::mem_fun(&MemFunTest::test2); <-- compile error, mem_fun doesn't work on binary fuctions
MemFunTest foo;
//fo0(); <-- compile error, as expected
//fo0(foo); <-- compile error, as expected
fo0(&foo);
//fo1(); <-- compile error, as expected
//fo1(&foo); <-- compile error, as expected
fo1(&foo, 3);
/*std::binder1st<Operation>*/ //std::bind1st(fo0, &foo); <-- compile error, since bind1st works only on binary fuctions
std::binder1st<std::mem_fun1_t<bool, MemFunTest, int> > bfo1 = std::bind1st(fo1, &foo);
//bfo1(); <-- compile error, as expected
bfo1(7);
return 0;
}
@Offirmo
Copy link
Author

Offirmo commented Feb 5, 2013

output of rev 1 with gcc 4.1.2 20080704 (Red Hat 4.1.2-48) :
[0x7fffd475bd6f-test0 called]
[0x7fffd475bd6f-test1(3) called]
[0x7fffd475bd6f-test1(7) called]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment