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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With