#include
#include
#include
#include
using namespace std;
template
class Execute
{
public:
template
static void execute(F&&f, T&&t, Ps&&...ps)
{
Execute::execute(f, std::forward(t), std::get(std::forward(t)), ps...);
}
};
template<>
class Execute<0>
{
public:
template
static void execute(F&&f, T&&t, Ps&&...ps)
{
f(ps...);
}
};
template
class ScopeGuard
{
public:
static ScopeGuard
MakeScopeGurad(Func func, Paras...para)
{
return ScopeGuard(std::move(func), std::move(para)...);
}
public:
ScopeGuard(Func&&f, Paras&&...p) :_func(f), _para(p...) { };
public:
~ScopeGuard()
{
Execute<::std::tuple_size>::value >::execute(_func, _para);
};
private:
Func _func;
tuple _para;
};
template
auto MakeScopeGurad(Func func, Paras...paras)->ScopeGuard
{
return ScopeGuard::MakeScopeGurad(func, paras...);
}
void FuncTest(int a, char b)
{
cout << "a:" << a << " b:" << b << endl;
}
void FuncTest1()
{
cout << "FuncTest1" << endl;
}
int main(int argc, char* argv[])
{
auto sg1 = MakeScopeGurad(FuncTest, 10, 'd');
auto sg2 = MakeScopeGurad(FuncTest1);
auto sg3 = MakeScopeGurad([](string str){cout << str << endl; }, "C++11 lambda function");
tuple<> tp = std::make_tuple();
cout << "------------------------" << endl;
return 0;
}