Pergunta

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);
    }
};

Is there a way to do this using template metaprogramming or something? I could always use a pointer to make it category* if that helps.

Foi útil?

Solução

Creating a generic findByName template is as simple as replacing the specific type with a template parameter:

template<class T>
struct findByName
{
    string name;

    bool operator()(const T &a)
    {
        return (a.name == name);
    }
};

(This assumes the parameter is passed by reference, but you could change it to take a pointer as the parameter if you prefer.)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top