From 062d4c54545280b5313ec8f83d3ed9bec7c4502b Mon Sep 17 00:00:00 2001 From: pezy chen Date: Fri, 11 May 2018 00:42:51 +0800 Subject: [PATCH] Added 16.37 ~ 41 --- ch16/README.md | 48 ++++++++++++++++++++++++++++++++- ch16/ex16.37.38.39/main.cpp | 53 ------------------------------------- ch16/ex16.40/main.cpp | 48 --------------------------------- 3 files changed, 47 insertions(+), 102 deletions(-) delete mode 100644 ch16/ex16.37.38.39/main.cpp delete mode 100644 ch16/ex16.40/main.cpp diff --git a/ch16/README.md b/ch16/README.md index b292391c..557095f2 100644 --- a/ch16/README.md +++ b/ch16/README.md @@ -329,4 +329,50 @@ Then, the answers: - (c) `T` is `const int*` - (d) `T1` and `T2` are both `const int*` - (e) **error**, `p1` is `int*`, `cp1` is `const int*`, they are different type -- (f) `T1` is `int*`, `T2` is `const int*` \ No newline at end of file +- (f) `T1` is `int*`, `T2` is `const int*` + +## Exercise 16.37 + +> The library `max` function has two function parameters and returns the larger of its arguments. This function has one template type parameter. Could you call `max` passing it an int and a double? If so, how? If not, why not? + +No, I could not. Because the arguments to `max` must have the same type. + +## Exercise 16.38 + +> When we call `make_shared` (12.1.1, p. 451), we have to provide an explicit template argument. Explain why that argument is needed and how it is used. + +Because when we call `make_shared`, it is allowed for no argument. Then, we have nothing to deduce the type of the return type. + +## Exercise 16.39 + +> Use an explicit template argument to make it sensible to pass two string literals to the original version of compare from 16.1.1 (p.652). + +```cpp +std::cout << compare("czwp", "czyz") << std::endl; + ^^^^^^^^^^^^^ +``` + +## Exercise 16.40 + +> Is the following function legal? If not, why not? If it is legal, what, if any, are the restrictions on the argument type(s) that can be passed, and what is the return type? + +```cpp +template +auto fcn3(It beg, It end) -> decltype(*beg + 0) { + // process the range + return *beg; // return a copy of an element from the range +} +``` + +legal. But only type that support this + 0 operation can be passed, and the return type depends on the what type the operator `+` return. + +## 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. + +```cpp +template +auto sum(T1 a, T2 b) -> decltype(a + b) { + return a + b; +} +``` \ No newline at end of file diff --git a/ch16/ex16.37.38.39/main.cpp b/ch16/ex16.37.38.39/main.cpp deleted file mode 100644 index 08b83c3b..00000000 --- a/ch16/ex16.37.38.39/main.cpp +++ /dev/null @@ -1,53 +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.37: -//! The library max function has two function parameters and returns the -//! larger of its arguments. This function has one template type parameter. -//! Could you call max passing it an int and a double? If so, how? If not, -//! why not? -// If so, it doesn't compile, because the two argument must be the same type or -// convertible. -// -//! Exercise 16.38: -//! When we call make_shared (§ 12.1.1, p. 451), we have to provide an -//! explicit template argument. Explain why that argument is needed and -//! how it is used. -//! -// without specified type given, make_shared has no possibility to -// to determine how big the size it should allocate, which is the reason. -// -// Depending on the type specified, make_shared allocates proper size of memory -// space and returns a proper type of shared_ptr pointing to it. -//! -//! Exercise 16.39: -//! Use an explicit template argument to make it sensible to pass two string -//! literals to the original version of compare from § 16.1.1 (p. 652). -//! - - -#include - - -template -int compare(const T &v1, const T &v2) -{ - if (v1 < v2) return -1; - if (v2 < v1) return 1; - return 0; -} - -int main() -{ - std::cout << compare("sss","aaa") << "\n"; - //! ^^^^^^^^^^^^^ - //! There is a normal conversion here, since it's an explicit argument. - - - -} diff --git a/ch16/ex16.40/main.cpp b/ch16/ex16.40/main.cpp deleted file mode 100644 index 5ac595c4..00000000 --- a/ch16/ex16.40/main.cpp +++ /dev/null @@ -1,48 +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.40: -//! Is the following function legal? If not, why not? If it is legal, what, -//! if any, are the restrictions on the argument type(s) that can be passed, -//! and what is the return type? -//! legal. -//! As shown below, only type that support this + 0 operation can be passed. -//! the return type depends on the what type the operator + returns. In the case -//! below, the return type is Bar. -//! - - -#include -#include -#include - -class Bar{}; - -Bar operator +(Bar lhs, int) -{ - return lhs; -} - -template -auto fcn3(It beg, It end) -> decltype(*beg + 0) -{ - return *beg; // return a copy of an element from the range -} - -int main() -{ - - std::vector v; - v.push_back(Bar()); - - Bar b = fcn3(v.begin(), v.end()); - ; - ; - - -}