Skip to content

Commit

Permalink
try to give a solution of pezy#135
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Sep 14, 2017
1 parent 51617db commit 023e41a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#vscode
.vs

# Mac OS
.DS_Store

Expand Down
30 changes: 0 additions & 30 deletions ch16/ex16.41/main.cpp

This file was deleted.

92 changes: 92 additions & 0 deletions ch16/ex16_41_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <climits>
#include <iostream>

template <typename T> struct promote;

template <> struct promote<short int> {
using type = int;
};

template <> struct promote<unsigned short int> {
using type = unsigned int;
};

template <> struct promote<int> {
using type = long long int;
};

template <> struct promote<unsigned int> {
using type = unsigned long int;
};

template <> struct promote<long int> {
using type = long long int;
};

template <> struct promote<unsigned long int> {
using type = unsigned long int;
};

template <> struct promote<long long int> {
using type = unsigned long long int;
};

template <> struct promote<unsigned long long int> {
using type = unsigned long long int;
};

template <> struct promote<float> {
using type = double;
};

template <> struct promote<double> {
using type = long double;
};

template <> struct promote<long double> {
using type = long double;
};

template <typename T> using promote_t = typename promote<T>::type;

template <typename T> auto sum(T lhs, T rhs) -> promote_t<T>
{
return static_cast<promote_t<T>>(lhs) + rhs;
}

int main()
{
std::cout << sum(std::numeric_limits<short int>::max(),
std::numeric_limits<short int>::max())
<< std::endl;
std::cout << sum(std::numeric_limits<unsigned short int>::max(),
std::numeric_limits<unsigned short int>::max())
<< std::endl;
std::cout << sum(std::numeric_limits<int>::max(),
std::numeric_limits<int>::max())
<< std::endl;
std::cout << sum(std::numeric_limits<unsigned int>::max(),
std::numeric_limits<unsigned int>::max())
<< std::endl;
std::cout << sum(std::numeric_limits<long int>::max(),
std::numeric_limits<long int>::max())
<< std::endl;
std::cout << sum(std::numeric_limits<unsigned long int>::max(),
std::numeric_limits<unsigned long int>::max())
<< std::endl;
std::cout << sum(std::numeric_limits<long long int>::max(),
std::numeric_limits<long long int>::max())
<< std::endl;
std::cout << sum(std::numeric_limits<unsigned long long int>::max(),
std::numeric_limits<unsigned long long int>::max())
<< std::endl;
std::cout << sum(std::numeric_limits<float>::max(),
std::numeric_limits<float>::max())
<< std::endl;
std::cout << sum(std::numeric_limits<double>::max(),
std::numeric_limits<double>::max())
<< std::endl; // too large
std::cout << sum(std::numeric_limits<long double>::max(),
std::numeric_limits<long double>::max())
<< std::endl; // too large
}

0 comments on commit 023e41a

Please sign in to comment.