The ninja employee The ninja employee - 1 month ago 9
C++ Question

How to efficiently overload a function, without going insane?

So I have this function (which has 80 lines):

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

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


and i have 20 different overloads of function platformInfo(); is there a way to make overloads of this function, where the only change is data type of foo, without copying the whole function 20 times?

Answer

With template:

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

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