#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;
}
test::~test()
が 2 回呼び出されるため、クラッシュします。ただし、 (or ) に置き換えるt
と、 main() を終了するときに 1 回だけ呼び出されます。std::cref(t)
std::ref(t), &t
~test()
理由がわかりませんでした。gcc 4.6.3を使用して、Ubuntu 12.04 64ビットを使用しています。