Skip to content

Commit

Permalink
finished to 16.27
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Apr 11, 2018
1 parent 4c5ce5c commit 14198d8
Show file tree
Hide file tree
Showing 35 changed files with 752 additions and 974 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BinPackParameters: true
BinPackArguments: true
ColumnLimit: 80
ColumnLimit: 100
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
DerivePointerAlignment: false
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#vscode
.vs
launch.json

# Mac OS
.DS_Store
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"miDebuggerPath": "D:\\libs\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
Expand Down
15 changes: 8 additions & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
{
"editor.formatOnSave": true,
"files.associations": {
"vector": "cpp",
"iosfwd": "cpp",
"random": "cpp",
"xstring": "cpp",
"bitset": "cpp",
"set": "cpp",
"tuple": "cpp",
"algorithm": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"cmath": "cpp",
"cstddef": "cpp",
Expand All @@ -26,6 +21,7 @@
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"iterator": "cpp",
Expand All @@ -37,18 +33,22 @@
"new": "cpp",
"numeric": "cpp",
"ostream": "cpp",
"random": "cpp",
"regex": "cpp",
"set": "cpp",
"sstream": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"utility": "cpp",
"vector": "cpp",
"xfacet": "cpp",
"xfunctional": "cpp",
"xhash": "cpp",
Expand All @@ -66,5 +66,6 @@
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp"
}
},
"C_Cpp.errorSquiggles": "Disabled"
}
76 changes: 71 additions & 5 deletions ch16/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,69 @@ When we want to inform the compiler that a name represents a type, we must use t
Fixed:
```cpp
(a) template <typename T, typename U, typename V> void f1(T, U, V);
(b) template <typename T> T f2(int &);
(c)
(a) template <typename T, typename U, typename V> void f1(T, U, V); // identifier 'U'
(b) template <typename T> T f2(int &); // typename would be hidden
(c) template <typename T> inline T foo(T, unsigned int*); // inline must be after template
(d) template <typename T> void f4(T, T); // return type should be provided
(e) typedef char Ctype;
template <typename T> T f5(Ctype a); // the typename hides this typedef
```
## Exercise 16.19
> Write a function that takes a reference to a container and prints the elements in that container. Use the container’s `size_type` and `size` members to control the loop that prints the elements.
[print](ex16_19_print_container.cpp)
## Exercise 16.20
> Rewrite the function from the previous exercise to use iterators returned from `begin` and `end` to control the loop.
[print](ex16_20_print_container_iter.cpp)
## Exercise 16.21
> Write your own version of `DebugDelete`.
[DebugDelete|h](ex16_21_debugdelete.h) | [DebugDelete|cpp](ex16_21_debugdelete.cpp)
## Exercise 16.22
> Revise your `TextQuery` programs from 12.3 (p. 484) so that the `shared_ptr` members use a `DebugDelete` as their deleter (12.1.4, p. 468).
[TestQuery|h](ex16_22_textquery.h) | [TestQuery|cpp](ex16_22_textquery.cpp) | [TestQuery|test](ex16_22_textquery_test.cpp)
## Exercise 16.23
> Predict when the call operator will be executed in your main query program. If your expectations and what happens differ, be sure you understand why.
when input the `q` to quit `runQueries` function. [Exercise 16.22](#exercise-1622)'s output can check this.
## Exercise 16.24
> Add a constructor that takes two iterators to your `Blob` template.
[Blob|h](ex16_24_blob.h) | [Blob|test](ex16_24_blob_test.cpp)
## Exercise 16.25
> Explain the meaning of these declarations
>```cpp
>extern template class vector<string>;
>template class vector<Sales_data>;
>```
`vector<string>` instantiation declaration here, it must be instantiated elsewhere in the program.
`vector<Sales_data>` instantiates all members of the class template here.
## Exercise 16.26
> Assuming `NoDefault` is a class that does not have a default constructor, can we explicitly instantiate `vector<NoDefault>`? If not, why not?
see <https://stackoverflow.com/questions/21525169/while-explicitly-instantiating-vectorsometype-what-is-the-sometype-default-co>
## Exercise 16.27
> For each labeled statement explain what, if any, instantiations happen. If a template is instantiated, explain why; if not, explain why not.
>```cpp
>template <typename T> class Stack { };
Expand All @@ -174,12 +223,29 @@ Fixed:
>```
Solution:
- (a) No instantiation, compiles, it got instantiated when called.
- (b) No instantiation, compiles, references and pointers doesn't need instantiation
- (c) Instantiation. Doesn't compile!
- (d) No instantiation, compiles, references and pointers doesn't need instantiation
- (e) Instantiation of Stack<char>. Doesn't compile!
- (f) Instantiation of Stack<std::string>. Doesn't compileNo instantiation, compiles, references and pointers doesn't need instantiation!
- (e) Instantiation of `Stack<char>`. Doesn't compile!
- (f) Instantiation of `Stack<std::string>`. Doesn't compileNo instantiation, compiles, references and pointers doesn't need instantiation!
Solution from [How is a template instantiated? - Stack Overflow](https://stackoverflow.com/questions/21598635/how-is-a-template-instantiated)
## Exercise 16.28
> Write your own versions of `shared_ptr` and `unique_ptr`.
## Exercise 16.29
> Revise your `Blob` class to use your version of `shared_ptr` rather than the library version.
## Exercise 16.30
Rerun some of your programs to verify your `shared_ptr` and revised `Blob` classes. (Note: Implementing the `weak_ptr` type is beyond the scope of this Primer, so you will not be able to use the `BlobPtr`
class with your revised `Blob`.)
## Exercise 16.31
>Explain how the compiler might inline the call to the deleter if we used `DebugDelete` with `unique_ptr`.
51 changes: 0 additions & 51 deletions ch16/ex16.17.18/main.cpp

This file was deleted.

62 changes: 0 additions & 62 deletions ch16/ex16.19.20/main.cpp

This file was deleted.

20 changes: 0 additions & 20 deletions ch16/ex16.21.22/DebugDelete.h

This file was deleted.

Loading

0 comments on commit 14198d8

Please sign in to comment.