@@ -3,39 +3,89 @@ cpp-tutor
3
3
[ ![ Build Status] ( https://travis-ci.org/banach-space/cpp-tutor.svg?branch=master )] ( https://travis-ci.org/banach-space/cpp-tutor )
4
4
[ ![ Build status] ( https://ci.appveyor.com/api/projects/status/axf91gjs67eoms4s/branch/add_appveyor?svg=true )] ( https://ci.appveyor.com/project/banach-space/cpp-tutor/branch/add_appveyor )
5
5
6
- Code examples that I use for tutoring C++.
6
+ Code examples for tutoring modern C++.
7
7
8
8
The intent of this tutorial is to give a quick overview of some of the most
9
- important and interesting features of C++, including new additions in the
10
- latest revision of the language (C++17 at the point of writing this). It is
11
- assumed that you already know how to program and are familiar with
12
- object-oriented design.
9
+ important and interesting features of C++, and to do so in a form of complete
10
+ and self-contained examples. It focuses on modern C++ (C++11/C++14/C++17), but
11
+ there are also some pre C++11 and C-specific examples for comparison. It's by
12
+ no means complete - quite the contrary. It aim is to be:
13
+ * ** Concise** : the examples are short, yet complete - to the point. There's a
14
+ source file implementing a ` main ` function for each item and you can study it
15
+ in complete isolation from other items.
16
+ * ** Selective** : the examples emphasise features and corner cases most likely
17
+ encountered when _ just_ starting with modern C++. They focus on key points
18
+ rather then presenting the complete picture. Less is more.
19
+ * ** Clear** : the examples sacrifice code quality in favour of code clarity
20
+ and readability, e.g. in some cases there's more comments than code. Also,
21
+ see the [ disclaimer] ( #disclaimer )
22
+
23
+ It is assumed that you already:
24
+ * know some basic C++
25
+ * are familiar with object-oriented programming
26
+
27
+ The code samples presented here will be helpful if you're switching to C++ from a
28
+ different object oriented language, are preparing for an interview, or, like
29
+ myself, are mentoring junior C++ developers.
13
30
14
31
Disclaimer
15
- ------------
32
+ ----------
16
33
The examples presented here are meant to highlight (and sometimes exploit) the
17
34
specifics and quirks of the language. To this end, the presented examples may
18
35
exhibit undefined or implementation specific behaviour, or implement solutions
19
36
that are against good coding practices. This is done for educational purposes
20
- only!
37
+ only and always thoroughly documented.
21
38
22
39
Status
23
- --------
40
+ ------
24
41
** WORK-IN-PROGRESS**
25
42
26
43
Platform Support
27
- --------
44
+ ----------------
28
45
The only requirement for ** cpp-tutor** is a C++17 compliant compiler. It is
29
- supported on Linux, Mac OS X and Windows and regularly tested on the following
30
- configurations (extracted from the CI files: ` .travis.yml ` and ` appveyor.yml ` ):
46
+ supported on Linux, Mac OS X and Windows and is regularly tested against the
47
+ following configurations (extracted from the CI files: ` .travis.yml ` and
48
+ ` appveyor.yml ` ):
31
49
* Linux Ubuntu 16.04 (GCC-7 and LLVM-7)
32
50
* Windows (Visual Studio 2015)
33
51
* Mac OS X 10.13 (Apple LLVM 10)
34
52
35
- Please refer to the instructions
53
+ Locally I used GCC-8.2.1 and LLVM-7 for development. Please refer to the CI
54
+ logs (links at the top of the page) for reference setups.
55
+
56
+ Usage
57
+ -----
58
+ The [ items] ( #tems ) discussed here are independent and you can study them in any
59
+ order that works for you. Once you choose an item that's of interest to you, go
60
+ through the links and code samples available here. Next,
61
+ [ build] ( #build-instructions ) and run it. Most binary files print to ` stdout ` .
62
+ Make sure that you understand where the output comes from, what it means that
63
+ and that it matches the comments in the code.
64
+
65
+ Some examples implement undefined behaviour, contain compiler errors or code
66
+ that leads to memory leaks. Such _ broken_ parts of the code are guarded off
67
+ with two preprocessor symbolic constants:
68
+ * ` COMPILATION_ERROR `
69
+ * ` MEMORY_LEAK `
70
+
71
+ Be default both constants are undefined and hence there are neither compilation
72
+ errors nor memory leaks. Play around by defining them (one at a time),
73
+ recompiling and re-running the examples. Make sure that the generated output
74
+ (or compiler errors) make sense. Comments in the corresponding source files
75
+ might be instrumental in understanding those.
76
+
77
+ You can (and should) use [ Valgrind] ( http://valgrind.org/ ) to get a better
78
+ grasp of memory leaks implemented in some of the examples, e.g.:
79
+ ```
80
+ $ cd <build_dir>
81
+ $ valgrind smart_pointers
82
+ ```
83
+ (` <build_dir> ` is the build directory used when [ building] ( #build-instructions )
84
+ the project). Remember to re-build and run ` Valgrind ` _ before_ and _ after_
85
+ defining ` MEMORY_LEAK ` .
36
86
37
87
Build Instructions
38
- --------
88
+ ------------------
39
89
It is assumed that ** cpp-tutor** will be built in ` <build-dir> ` and that the
40
90
top-level source directory is ` <source-dir> ` . For brevity, the build
41
91
instructions are presented for Linux only.
@@ -46,18 +96,29 @@ $ cd <source_dir>
46
96
$ git clone https://github.com/abseil/googletest.git
47
97
```
48
98
49
- Next, you can build as follows:
99
+ Next, you can build all the examples as follows:
50
100
```
51
101
$ cd <build-dir>
52
102
$ cmake <source_dir>
53
103
$ make
54
104
```
55
- This will generate all the targets implemented for this project.
105
+ This will generate all the targets implemented for this project. If you want to
106
+ (re-)build a particular example, run:
107
+ ```
108
+ $ make <example_name>
109
+ ```
110
+ In order to define either ` COMPILATION_ERROR ` or ` MEMORY_LEAK ` , re-run CMake:
111
+ ```
112
+ $ cmake -DCOMPILATION_ERROR=1 .
113
+ ```
114
+ This will update ` <build_dir>/include/cpp_tutor.h ` , which is auto-generated by
115
+ CMake.
56
116
57
- Content
117
+ Items
58
118
--------
59
119
The items covered in this tutorial so far (with some relevant links):
60
- 1 . Strings
120
+ 1 . [ Strings] ( http://cs.stmarys.ca/~porter/csc/ref/c_cpp_strings.html ) (even more
121
+ [ strings] ( https://embeddedartistry.com/blog/2017/7/24/stdstring-vs-c-strings ) )
61
122
* C-strings vs ` std::string ` vs ` std::string_view `
62
123
* the underlying data-representation
63
124
* SSO ([ Short String Optimisation] ( https://akrzemi1.wordpress.com/2014/04/14/common-optimizations/ ) )
@@ -68,9 +129,9 @@ The items covered in this tutorial so far (with some relevant links):
68
129
* TODO: ` malloc\calloc\realloc\free `
69
130
* all forms of ` new ` and ` delete ` (for plain datatypes and classes)
70
131
* dynamic array of dynamic objects (a.k.a. 2-dimensional dynamical arrays)
71
- * memory leaks caused by mismatch in ` new ` and ` delete ` used
132
+ * memory leaks caused by mismatch in ` new ` and ` delete `
72
133
* deep vs shallow copy
73
- * a basic memory manager with the aid of ` placement new `
134
+ * a basic memory manager implemented in terms of ` placement new `
74
135
* source files:
75
136
* ` pointers.cpp ` , ` strings_object.cpp ` , ` strings_object.hpp ` ,
76
137
` strings_object_main.cpp ` , ` tests_strings_object.cpp ` ,
@@ -86,7 +147,7 @@ The items covered in this tutorial so far (with some relevant links):
86
147
* ` std::move ` vs ` std::forward `
87
148
* source files:
88
149
* ` rvalue_vs_lvalue_main.cpp `
89
- 5 . Move semantics
150
+ 5 . [ Move semantics] ( https://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html )
90
151
* move constructor and move assign operator
91
152
* source files
92
153
* ` memory_block.cpp ` , ` memory_block_main.cpp `
@@ -95,7 +156,8 @@ The items covered in this tutorial so far (with some relevant links):
95
156
* guaranteed copy elision (C++17)
96
157
* source files:
97
158
* ` rvo_main.cpp `
98
- 7 . New kewords in modern C++:
159
+ 7 . [ New kewords in C++11] ( https://www.codeproject.com/Articles/570638/Ten-Cplusplus11-Features-Every-Cplusplus-Developer )
160
+ and [ beyond] ( https://github.com/AnthonyCalandra/modern-cpp-features ) :
99
161
* ` const ` vs ` constexpr ` , ` nullptr ` , ` auto ` , ` decltype `
100
162
* source files:
101
163
* ` const_vs_constexpr_main.cpp ` , ` null_vs_nullptr_main.cpp ` ,
@@ -105,7 +167,7 @@ License
105
167
--------
106
168
The MIT License (MIT)
107
169
108
- Copyright (c) 2018 Andrzej Warzyński
170
+ Copyright (c) 2018-2019 Andrzej Warzyński
109
171
110
172
Permission is hereby granted, free of charge, to any person obtaining a copy of
111
173
this software and associated documentation files (the "Software"), to deal in
0 commit comments