boost bind配合weak_ptr,boost bind无法直接绑定lambda,但是lambda赋值给function以后可以绑定

boost bind配合weak_ptr,boost bind无法直接绑定lambda,但是lambda赋值给function以后可以绑定
// boost_test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <boost/smart_ptr.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
class CTest
{
public:
virtual void TestShow()
{
std::cout << __FUNCTION__ << std::endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
typedef boost::function<void()> TVoidEvent;
std::vector<TVoidEvent> vVoidEvent;
for (int i = 0; i < 9; ++i)
{
boost::shared_ptr<CTest> spTest = boost::make_shared<CTest>();
boost::weak_ptr<CTest> wpTest(spTest);
//TVoidEvent voidEvent = boost::bind(
// [/*wpTest*/]()
// {
// //boost::shared_ptr<CTest> spTestTem = wpTest.lock();
// //if(spTestTem)
// //{
// // spTestTem->TestShow();
// //}
// std::cout << __FUNCTION__ << std::endl;
// }
// );
//TVoidEvent voidEvent = [spTest]
// {
// if (spTest)
// {
// spTest->TestShow();
// }
// };
// bind配合weak_ptr使用
//TVoidEvent voidEvent = [wpTest]
//{
// boost::shared_ptr<CTest> spTem = wpTest.lock();
// if (spTem)
// {
// spTem->TestShow();
// }
//};
// 直接把lambda和bind一起用编译有问题
TVoidEvent voidEvent = boost::bind(
[wpTest]
{
boost::shared_ptr<CTest> spTem = wpTest.lock();
if (spTem)
{
spTem->TestShow();
}
}
);
vVoidEvent.push_back(voidEvent);
}
std::for_each(vVoidEvent.begin(), vVoidEvent.end(),
[](TVoidEvent& voidEvent)
{
voidEvent();
});
return 0;
}


上面能过,下面就过不了,,,模板V5
               []{std::cout << __FUNCTION__ << std::endl;}();
boost::bind([]{std::cout << __FUNCTION__ << std::endl;});

你可能感兴趣的:(boost bind配合weak_ptr,boost bind无法直接绑定lambda,但是lambda赋值给function以后可以绑定)