如何将各类函数作为容器进行管理
How to manage various types of functions as containers
我正在尝试通过 C++ 模板管理函数列表。
template<typename T, typename... Args>
std::map<std::wstring, std::function<T(Args...)>> mFuncs;
上面的代码不是正确的句子,但是和上面的代码概念一样,我想用一个List来管理各种类型的函数
void RegisterFuncs()
{
mFuncs.emplace(L"Func1", [&]()->bool{ ... });
mFuncs.emplace(L"Func2", [&](int a, std::string b)->int{ ... });
mFuncs.emplace(L"Func3", [&](std::tuple<int,int>& a){ ... });
...
...
...
mFuncs.emplace(L"Func", [&](args...)->auto{ ... });
}
template<typename... TArgs>
inline auto CallFunc(const wchar_t* pKey, TArgs&&... pArgs)
{
...
...
return mFuncs[pKey](std::forward<TArgs>(pArgs)...);
}
如何实现上述概念?
使用std::any
std::map<std::string, std::any> f;
int i = 3;
void RegisterFuncs()
{
f.emplace("func1", std::function<double(int)>([&](int k){i = k; return 5.0; }));
f.emplace("func2", std::function<bool()>([](){return false; }));
// ^^^^^^^^^^^^^^^^ cant use lambda type becuase any_cast doesnt support implicit casting
}
template<typename R, typename... Args>
inline R CallFunc(const std::string& name, Args&&... args)
{
return std::any_cast<std::function<R(Args...)>>(f[name])(args...);
}
int main()
{
RegisterFuncs();
double r = CallFunc<double, int>("func1", 6);
std::cout << "r: " << r << " i: " << i << "\n";
bool b = CallFunc<bool>("func2");
std::cout << std::boolalpha << "b: " << b << "\n";
//types must match exactly or bad_any_cast is thrown
try
{
int c = CallFunc<int>("func2");
std::cout << "c: " << c << "\n";
}
catch(std::bad_any_cast e)
{
std::cout << e.what();
}
}
我正在尝试通过 C++ 模板管理函数列表。
template<typename T, typename... Args>
std::map<std::wstring, std::function<T(Args...)>> mFuncs;
上面的代码不是正确的句子,但是和上面的代码概念一样,我想用一个List来管理各种类型的函数
void RegisterFuncs()
{
mFuncs.emplace(L"Func1", [&]()->bool{ ... });
mFuncs.emplace(L"Func2", [&](int a, std::string b)->int{ ... });
mFuncs.emplace(L"Func3", [&](std::tuple<int,int>& a){ ... });
...
...
...
mFuncs.emplace(L"Func", [&](args...)->auto{ ... });
}
template<typename... TArgs>
inline auto CallFunc(const wchar_t* pKey, TArgs&&... pArgs)
{
...
...
return mFuncs[pKey](std::forward<TArgs>(pArgs)...);
}
如何实现上述概念?
使用std::any
std::map<std::string, std::any> f;
int i = 3;
void RegisterFuncs()
{
f.emplace("func1", std::function<double(int)>([&](int k){i = k; return 5.0; }));
f.emplace("func2", std::function<bool()>([](){return false; }));
// ^^^^^^^^^^^^^^^^ cant use lambda type becuase any_cast doesnt support implicit casting
}
template<typename R, typename... Args>
inline R CallFunc(const std::string& name, Args&&... args)
{
return std::any_cast<std::function<R(Args...)>>(f[name])(args...);
}
int main()
{
RegisterFuncs();
double r = CallFunc<double, int>("func1", 6);
std::cout << "r: " << r << " i: " << i << "\n";
bool b = CallFunc<bool>("func2");
std::cout << std::boolalpha << "b: " << b << "\n";
//types must match exactly or bad_any_cast is thrown
try
{
int c = CallFunc<int>("func2");
std::cout << "c: " << c << "\n";
}
catch(std::bad_any_cast e)
{
std::cout << e.what();
}
}