지돌이의 블로그 입니다!

될줄 알았죠?

안돼요...ㅠㅠ

이 글을 보면 해결책이 있을줄 알았죠?

없어요...^^ 최소한 C++ 자체 라이브러리 안에서는....ㅠㅠ

아래와 같이 따로 만들면은 됩니다!

#include <iostream>
#include <memory>
#include <functional>
#include <tuple>

class Caller {
public:
	virtual void run() = 0;
};

template<typename F, typename Tuple>
class Binder : public Caller
{
protected:
	F f_;
	std::size_t args_count_
		= std::tuple_size<typename std::remove_reference<Tuple>::type>::value;
	Tuple tuple_;

public:
	Binder(F f, Tuple&& tuple) : f_(f), tuple_(std::move(tuple)) {
		std::cout << "args_count_ : " << args_count_ << std::endl;
	}
	
	template<typename F, typename Tuple, size_t ...S >
	void run_impl(F&& fn, Tuple&& t, std::index_sequence<S...>)
	{
		return std::forward<F>(fn)(std::get<S>(std::forward<Tuple>(t))...);
	}

	void run() override {
		std::size_t constexpr tSize
			= std::tuple_size<typename std::remove_reference<Tuple>::type>::value;

		run_impl(std::forward<F>(f_),
			std::forward<Tuple>(tuple_),
			std::make_index_sequence<tSize>());
	}
};

template<class F, class... Args>
std::unique_ptr<Caller> mybind(F&& f, Args ... args) {
	return std::make_unique<Binder<F, std::tuple<Args...>>>(f, std::make_tuple(std::forward<Args>(args)...));
}

int main()
{
	std::unique_ptr<std::string> temp(new std::string("TEST1234"));

	std::unique_ptr<Caller> caller = mybind([](std::unique_ptr<std::string> temp, int a, int b) {
		std::cout << *temp << std::endl;
		}, std::move(temp), 10, 20);

	caller->run();

	return 0;
}

Comment +0