mardi 28 novembre 2017

Covering legacy C style functions using C++ template

I have the following two functions in C.

float floatAdd(float a, float b)
{
    return a+b;
}

double doubleAdd(double a, double b)
{
    return a+b;
}

Now, I would like to combine both functions and write a tempalte function in C++ as follows.

template<class T>
T add(T a, T b)
{
    // if T=float, return floatAdd(a,b)
    // if T=double, return doubleAdd(a,b)

}

Since the return types are different, I am unable to find a solution!

Aucun commentaire:

Enregistrer un commentaire