#include <functional>
#include <iostream>
using namespace std;
class test {
public:
test(){ p = new int[10];}
void print_info(double a)
{
cerr << a << endl;
}
~test(){
cerr << __LINE__ << endl;
delete []p;
}
private:
int *p;
};
int main()
{
test t;
std::function<void(void)> f = std::bind(&test::print_info, t, 2.0);
//std::function<void(void)> f = std::bind(&test::print_info, std::cref(t), 2.0);
return 0;
}
It will crash, since test::~test()
is called twice. But, if I replace t
with std::cref(t)
(or std::ref(t), &t
), ~test()
will be called only once when exiting main().
I didn't figure out the reason. I'm on Ubuntu 12.04 64bit, using gcc 4.6.3.