From 023e41a5af4d50bc3825429c74ca17705d8ca120 Mon Sep 17 00:00:00 2001 From: pezy Date: Fri, 15 Sep 2017 00:39:03 +0800 Subject: [PATCH] try to give a solution of #135 --- .gitignore | 3 ++ ch16/ex16.41/main.cpp | 30 -------------- ch16/ex16_41_sum.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 30 deletions(-) delete mode 100644 ch16/ex16.41/main.cpp create mode 100644 ch16/ex16_41_sum.cpp diff --git a/.gitignore b/.gitignore index e84bed97..c8a168df 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +#vscode +.vs + # Mac OS .DS_Store diff --git a/ch16/ex16.41/main.cpp b/ch16/ex16.41/main.cpp deleted file mode 100644 index 2b6a0732..00000000 --- a/ch16/ex16.41/main.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/*************************************************************************** - * @file main.cpp - * @author Alan.W - * @date 07 Feb 2014 - * @remark This code is for the exercises from C++ Primer 5th Edition - * @note - ***************************************************************************/ -//! -//! Exercise 16.41: -//! Write a version of sum with a return type that is guaranteed to be large -//! enough to hold the result of the addition. -//! - - -#include -#include -#include - -template -auto sum(T lhs, T rhs) -> decltype( lhs + rhs) -{ - return lhs + rhs; -} - -int main() -{ - auto s= sum(123456789123456789123456789123456789123456789, 123456789123456789123456789123456789123456789) ; - ; - ; -} diff --git a/ch16/ex16_41_sum.cpp b/ch16/ex16_41_sum.cpp new file mode 100644 index 00000000..d0428ed2 --- /dev/null +++ b/ch16/ex16_41_sum.cpp @@ -0,0 +1,92 @@ +#include +#include + +template struct promote; + +template <> struct promote { + using type = int; +}; + +template <> struct promote { + using type = unsigned int; +}; + +template <> struct promote { + using type = long long int; +}; + +template <> struct promote { + using type = unsigned long int; +}; + +template <> struct promote { + using type = long long int; +}; + +template <> struct promote { + using type = unsigned long int; +}; + +template <> struct promote { + using type = unsigned long long int; +}; + +template <> struct promote { + using type = unsigned long long int; +}; + +template <> struct promote { + using type = double; +}; + +template <> struct promote { + using type = long double; +}; + +template <> struct promote { + using type = long double; +}; + +template using promote_t = typename promote::type; + +template auto sum(T lhs, T rhs) -> promote_t +{ + return static_cast>(lhs) + rhs; +} + +int main() +{ + std::cout << sum(std::numeric_limits::max(), + std::numeric_limits::max()) + << std::endl; + std::cout << sum(std::numeric_limits::max(), + std::numeric_limits::max()) + << std::endl; + std::cout << sum(std::numeric_limits::max(), + std::numeric_limits::max()) + << std::endl; + std::cout << sum(std::numeric_limits::max(), + std::numeric_limits::max()) + << std::endl; + std::cout << sum(std::numeric_limits::max(), + std::numeric_limits::max()) + << std::endl; + std::cout << sum(std::numeric_limits::max(), + std::numeric_limits::max()) + << std::endl; + std::cout << sum(std::numeric_limits::max(), + std::numeric_limits::max()) + << std::endl; + std::cout << sum(std::numeric_limits::max(), + std::numeric_limits::max()) + << std::endl; + std::cout << sum(std::numeric_limits::max(), + std::numeric_limits::max()) + << std::endl; + std::cout << sum(std::numeric_limits::max(), + std::numeric_limits::max()) + << std::endl; // too large + std::cout << sum(std::numeric_limits::max(), + std::numeric_limits::max()) + << std::endl; // too large +}