我正在尝试编写一个程序,它将根据我从数据库中获取的字符串执行一个函数。 基本上我所做的是:
// Create an enum
enum AFunc{
invalidFunction,
function2,
function3
}
// have a class handling the functions
struct A
{
static AFunc resolveStringToFunction(std::string) {...}
template<int T>
void execute(...)
{
// this may not be called = invalidFunction
}
template<>
void execute<1> (...)
{
// do stuff = function1
}
template<>
void execute<2> (...)
{
// do stuff = function2
}
};
在我的申请中,我这样做:
A a;
std::string funcString = getFromDatabase (...) // Not really, but this is abstract
const AFunc funcType = A::resolveStringToFunction(funcString);
a.execute<funcType>(...);
这里的问题是编译器不接受动态调用模板函数,因为(据我所知)它需要知道编译时调用哪个函数。
有什么方法吗?
这个问题有更好的解决方案吗?也许是一种设计模式?
答案 0 :(得分:0)
<t t-foreach="partners" t-as="partner">
<t t-set="p_inovices" t-value=" list ( partner ) " />
</t>
它由以下代码调用:
namespace afunc // dont pollute global namespace
{
typedef std::function<void(...)> aFunc;
static function1(...)
{
// do stuff
}
static function2(...)
{
// do stuff
}
const std::unordered_map<std::string, aFunc> functions (
{{"function1", &function1},{"function2", &function2}} ); // afunc