# Regular Meeting (2020.4.27)
## Outline
* Reading code about
* Lost `trg/trg/Utilities.h`, so can't compile
* only read apparent
* detailing about algorithm I'll discuss by Wensday.
## Feature of code
* [ Original code on here](https://github.com/curly-wei/HT)
* Off-line calculated
* Base guarantee (see [Appendex A1](###A1.-Guarantee-of-code))
* ostream
* Don't know what guarantee `Utilities.h` can provied
## 2. Where could be improved (Modren C++)
### 2.1 Performance
#### 2.1.1 compile time computing
* `const` -> `constexpr` (C++ 11, gcc > 5)
* without constexpr

* using constexpr

* [refer to here](https://medium.com/@tjsw/%E6%BD%AE-c-constexpr-ac1bb2bdc5e2)
* `if` -> `if constexpr` (C++ 17, gcc > 7.3)
* [refer to here](https://medium.com/@tjsw/%E6%BD%AE-c-17-constexpr-if-%E7%B7%A8%E8%AD%AF%E6%9C%9F%E5%B0%B1%E7%9F%A5%E9%81%93%E7%B5%90%E6%9E%9C%E7%9A%84-if-fc6b6db7864b)
**When performance of C++ absolute over C, that's here**
* `move` sentence
### 2.2 Better coding style
* `unsigned` -> `uint_32` or `uint_16` in `cstdint.h`
* `unsigned` means `long` in almost 64-bits C/C++ compileres
* `long` -> `32` bits ( 64bits POSIX OS, such as Linu, Mac, Andorid, ios)
* `long` -> `64` bits (64bits Windows)
* Don't always use `std::endl` as **line break**, we shoud always use `\n`
* `std::endl` means **change line** and **purge I/O buffer**, it can leads performance bad
* refer to [here](http://chino.taipei/note-2016-0311C-%E7%9A%84%E8%BC%B8%E5%87%BA%E5%85%A5cin-cout%E5%92%8Cscanf-printf%E8%AA%B0%E6%AF%94%E8%BC%83%E5%BF%AB%EF%BC%9F/)
## Appendex
### A1. Guarantee of code
#### A1.1 Nothrow
Should not throw expection.
e.g.
* std::swap(T& A, T& b)
* `+`, `-`, `*`, `/`... basic airthmatic
#### A1.2 Basic guarantee
When exception was thrown,
**data could be destroyed**,
but **no resource leak**,
program execute normally until end.
e.g.
* `Data add_bias(Data& input)`
* input data: `[1, 2, 3, ...., 10]`
* handling: all emements of the data `+5`, and `5` must generated by **dynamically** memory allocate
* means **memory leak may occure** if we don't execute destructor correctly
* correct output: `[6, 7, 8, ...., 15]`
* **possiable error output with base gararntee:** `[6, 7, 3, 4, 5, ...., 10]`
* appearance:
some error lead element of input data without executed `+5` after index[3]
* but program execute normally until end, **no resource leaks**
#### A1.3 Strong guarantee
When exception was thrown,
not only **no resource leak**,
but also data **data don't be destroyed**,
in error occured,
everything seems to not happen while function executes to end.
e.g
* using example of A1-2 [`Data add_bias(Data& input)`](###A1.2-Basic-exception-guarantee):
* input data: `[1, 2, 3, ...., 10]`
* handling: all emements of the data `+5`, and `5` must generated by **dynamically** memory allocate
* means **memory leak may occure** if we don't execute destructor correctly
* correct output: `[6, 7, 8, ...., 15]`
* **possiable error output with base gararntee:** `[1, 2, 3, ...., 10]`
* appearance:
seems to not happen while function executes to end
* no resource leaks
* `std::vector.push_back()` in C++
#### A1.4 Example
If we write a myself program module_C:
```C++
module-C -> Base guarntee
```
which invoke 2 3rd-party modules:
```C++
module_A -> Nothrow
module_B -> Strong exception guarantee
```
So,
What's guarntee of my program?
```C++
// my program
module_C {
// some of my self code...
invoke module_A()
invoke module_B()
// some of my self code...
}
```
Ans: `Base guarntee`
#### A1.5 Summery
* To implement `Nothrow`
* too hard too fantasy(夢幻)
* has `memory allocation`, with possible of `memory leak`
* only **swap** function and **basic airthmatic** such as `+`, `-` can arrived(or said: they **must** provide **Nothrow guarantee**)
* because date input of `swap` or `+` or `-` or ... ,
should be allocated fine to ready for functions(`swap` or `+` or `-` or ...).
* To implement `Strong guarntee`
* Performance is bad on the almost instance
* some example shows strong guarntee
ex1:
```C++
Data_t func (Data_t& input_data){
Data_t buffer = input_data // cost of copy too large
return do_something_could_occured_exception(buffer);
}
output = func(input)
```
ex2:
```C++
//func_strong_guarntee
void change_image(Image_t& myself, Image_t& input){
Image_t Orig_img& = myself.img;
myself.img = new Image_t(input);
// if exception occured in new Image_t(input),
// myself.img can't be destroyed
delete Orig_img;
}
// effective C++, p55
```
* To implement `Basic guarntee`
* when you upload your code to general opensource, such as opencv, your code **must** satisfy `Basic guarntee` **at least**
###### tags: `Regular Meeting` `DeWei`