Skip to content

Instantly share code, notes, and snippets.

@nixiz
Created May 12, 2020 09:49
Show Gist options
  • Save nixiz/0e9bf8bc72352fee6ac7587b9cc46833 to your computer and use it in GitHub Desktop.
Save nixiz/0e9bf8bc72352fee6ac7587b9cc46833 to your computer and use it in GitHub Desktop.
#include <iostream>
namespace dtl // detail
{
template <typename T1, typename T2 = T1>
struct carpma_t {
typedef T1 tipi;
};
template <>
struct carpma_t<int> {
typedef int tipi;
};
template <>
struct carpma_t<double> {
typedef double tipi;
};
template <>
struct carpma_t<int, double> {
typedef double tipi;
};
template <>
struct carpma_t<double, int> {
typedef double tipi;
};
template <typename T1, typename T2 = T1>
struct toplama_t {
typedef T1 tipi;
};
template <>
struct toplama_t<int> {
typedef int tipi;
};
template <>
struct toplama_t<double> {
typedef double tipi;
};
template <>
struct toplama_t<int, double> {
typedef double tipi;
};
template <>
struct toplama_t<double, int> {
typedef double tipi;
};
template <typename T1, typename T2 = T1>
struct cikarma_t : toplama_t<T1, T2> { };
template <typename T1, typename T2 = T1>
struct bolme_t : carpma_t<T1, T2> { };
} // namespace detail
template <typename T1, typename T2>
struct donus_tipi {
typedef typename dtl::carpma_t<T1, T2>::tipi carpma;
typedef typename dtl::toplama_t<T1, T2>::tipi toplama;
typedef typename dtl::bolme_t<T1, T2>::tipi bolme;
typedef typename dtl::cikarma_t<T1, T2>::tipi cikarma;
};
template <typename T>
struct tip_traits {
static const char* tip_adi;
};
template <typename T>
const char* tip_traits<T>::tip_adi = "bilinmiyor";
template <>
const char* tip_traits<int>::tip_adi = "int";
template <>
const char* tip_traits<double>::tip_adi = "double";
template <typename type1, typename type2>
typename donus_tipi<typename donus_tipi<type1, type2>::toplama,
typename donus_tipi<type1, type2>::cikarma>::carpma
islem(type1 a, type2 b)
{
typedef typename donus_tipi<type1, type2>::cikarma cikarma_tipi;
typedef typename donus_tipi<type1, type2>::toplama toplama_tipi;
typedef typename donus_tipi<toplama_tipi, cikarma_tipi>::carpma dt;
dt sonuc = a + b * (a - b);
std::cout
<< "islem sonucu ("
<< tip_traits<dt>::tip_adi << "): "
<< sonuc << "\n";
return sonuc;
}
// compiler explorer link: https://gcc.godbolt.org/z/gJqRgH
int main(int argc, const char* argv[])
{
int x = islem(7, 3);
double y = islem(7., 3);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment