mardi 15 mars 2022

How to template by-value returning value and by-const-ref returning const ref

Is there a way to have those two cases as a single template:

// accepts by-value, returns value
int f(bool cond, int a, int b) {
    return cond ? a : b;
}

// accepts by-const-ref, returns const ref
const std::string& f(bool cond, const std::string& a,
                     const std::string& b) {
    return cond ? a : b;
}

Using at least C++11.

Aucun commentaire:

Enregistrer un commentaire