Joo*_*kia -1 c++ template-meta-programming
struct findCategoryByName
{
string name;
bool operator()(const category& a)
{
return (a.name == name);
}
};
struct findEntryByName
{
string name;
bool operator()(const entry* a)
{
return (a->name == name);
}
};
Run Code Online (Sandbox Code Playgroud)
有没有办法使用模板元编程或其他东西?如果有帮助,我总是可以使用指针使其成为类别*.
创建通用findByName模板就像使用模板参数替换特定类型一样简单:
template<class T>
struct findByName
{
string name;
bool operator()(const T &a)
{
return (a.name == name);
}
};
Run Code Online (Sandbox Code Playgroud)
(这假设参数是通过引用传递的,但如果您愿意,可以将其更改为将指针作为参数.)