samedi 3 septembre 2016

How correctly to remove code repetition in template function

I have code like this ("spaceship"-like operator).

template <class T>
int comparator(const T &a, const T &b){
    if (a < b){
        return -1;
    }else if (a > b){
        return +1;
    }

    return 0;
}

inline int comparator(const char *a, const char *b){
    return strcmp(a, b); // I never tried this, included just to get the idea
}

inline int comparator(char const a, char const b){
    return a - b;
}

inline int comparator(int const a, int const b){
    return a - b;
}

How can I easily remove repetition for several signed types (char, short, int, long etc). I tried with SFINAE, but result was not very encouraging.

Aucun commentaire:

Enregistrer un commentaire