vendredi 28 octobre 2016

return different data type without explicitly specify data type in template

I want to do something like this:

template<class T>
T foo(uint8_t x)
{
    if (x<32) return (int32_t)(1<<x);
    else return (int64_t)(1<<x);
}

but I do not want to call foo<int32_t>(x) or foo<int64_t>(x). It just doesn't look nice.I want the return type to be automatically deduced correctly.

If macros can achieve the same, I'm happy to use macros. Any suggestions?

Aucun commentaire:

Enregistrer un commentaire