이것은) (펑터를 무효화하는 특정,하지만 나와 함께 괜찮C++ 약한 펑 솔루션
struct Foo
{
void Bar(int x)
{
std::cout << x << std::endl;
}
};
struct VoidBind
{
typedef void result_type;
template<typename T> void operator()(const std::weak_ptr<T>& obj, std::function<void()>& func)
{
std::shared_ptr<T> shared_obj = obj.lock();
if (shared_obj)
{
func();
}
}
template<typename T> static std::function<void()> bind(const std::shared_ptr<T>& obj, const std::function<void()>& func)
{
return std::bind(VoidBind(), std::weak_ptr<T>(obj), func);
}
};
#define BIND(F, O, A...) VoidBind::bind(O, std::function<void()>(std::bind(F, O.get(), ##A)))
이 코드는 다음과 같이 호출 ...
auto obj = std::make_shared<Foo>();
auto func = BIND(&Foo::Bar, obj, 99); // match std::bind style
func();
obj.reset(); // destroy object
func(); // will not do anything
내 질문 BIND 매크로를 피할 수있는 방법이 있는지 여부입니다.
그게 작동합니다. 감사. – user1715664