Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make a function return a typename?

I was working on a project recently and wanted to give precedence to certain types when performing operations other than what is standard. To do this I was attempting to use a template somehow to determine what kind of datatypes were being used. The code I have written clearly doesn't work, but it gets the idea of what I'm trying to do across

#include <iostream>

template <type1,type2>
typename determine(type1 a, type2 b)
{
    if (typeid(type1) == typeid(int) || typeid(type2) == typeid(int))
        return int;
    else return double;
}

int main()
{
    int a = 3;
    double b = 2;
    std::cout << (static_cast<determine(a, b)>(a) / static_cast<determine(a, b)>(b)) << std::endl;
}

Is there a way to make determine return something that I can actually use to decide what datatype to use?

like image 921
rtpax Avatar asked Apr 10 '16 05:04

rtpax


1 Answers

You can use template metaprogramming techniques to accomplish your goal.

template <typename T1, typename T2> struct TypeSelector
{
   using type = double;
};

template <typename T1> struct TypeSelector<T1, int>
{
   using type = int;
};

template <typename T2> struct TypeSelector<int, T2>
{
   using type = int;
};

template <> struct TypeSelector<int, int>
{
   using type = int;
};

and then, use:

int main()
{
    int a = 3, b = 2;
    using type1 = TypeSelector<decltype(a), decltype(b)>::type;
    std::cout << (static_cast<type1>(a) / static_cast<type1>(b)) << std::endl;

   float c = 4.5f;
   using type2 = TypeSelector<decltype(a), decltype(c)>::type;
   std::cout << (static_cast<type2>(a) / static_cast<type2>(c)) << std::endl;

   using type3 = TypeSelector<decltype(c), decltype(a)>::type;
   std::cout << (static_cast<type3>(c) / static_cast<type3>(a)) << std::endl;

}
like image 82
R Sahu Avatar answered Sep 17 '22 19:09

R Sahu