dimanche 17 octobre 2021

Deduce datatype based on the input datatype in C++

Is there a way to deduce the datatype from the input to the function in C++. Example:

template<typename T> func(T input){
if((std::is_same<T, float>::value))
     uint32_t compute;
else if((std::is_same<T, double>::value))
    uint64_t compute;

// rest of the function which uses compute value to do necessary computation.

}

I understand with the current declaration the scope of the variable dies after the if condition loop. So i have added a func_compute and calling it from each if condition. Wanted to know if there is a way to do it much cleaner way.

Aucun commentaire:

Enregistrer un commentaire