如何有效地重载函数而不发疯?

How to efficiently overload a function, without going insane?

所以我有这个函数(有 80 行):

int listPlatformInfo(..., char * foo)
{
    ... 

    for (uint32_t a = 0; a < platformCount; a++)
    {
        platformInfo(platforms, info, foo);
    }
    return 0;
}

我有 20 个不同的函数重载 platformInfo(); 有没有办法重载这个函数,唯一的变化是 foo 的数据类型,而无需复制整个函数20次?

使用模板:

template<typename T>
int listPlatformInfo(..., T foo) // or T* ?
{
    ... 

    for (uint32_t a = 0; a < platformCount; a++)
    {
        platformInfo(platforms, info, foo);
    }
    return 0;
}

这正是存在泛型的原因。见 template functions:

    template<class T>
    void myGenericFunction(T parameter)
    {
        cout << parameter << " is of type "<< typeid(parameter).name() << endl;
    }

    int main()
    {
        myGenericFunction<int>(1);
        return 0;
    }