Title: Theory / Unimportant / CS / Programming / C++ / Manual / Norm / 11 Select Operation;
Tags: seen_description syntactical_sugar
On every note of this deck, unless a note says otherwise:
Language: C++
Chapter: 11

Instruction: expr_1 & expr_2
Page: 274
Section: 1.2
Returns: The binary and of expr_1 and expr_2
Variables: expr_i an expression of integral type

Instruction: expr_1 | expr_2
Page: 274
Section: 1.2
Returns: The boolean and of expr_1 or expr_2
Variables: expr_i an expression of integral type

Instruction: ~expr
Page: 274
Section: 1.2
Returns: The binary negation of expr_1
Variables: expr an expression of integral type

Instruction: expr_1 >> expr_2
Page: 274
Section: 1.2
Returns: expr_1 shifted right by expr_2 bits
Variables: expr an expression of integral type

Instruction: expr_1 << expr_2
Page: 274
Section: 1.2
Returns: expr_1 shifted left by expr_2 bits
Variables: expr an expression of integral type

Instruction: var += 1
Page: 276
Effect: Add 1 to var
Section: 1.4
Returns: var + 1
Variables: var an integral variable
Abbreviation: ++var

Instruction: var -= 1
Page: 276
Effect: remove 1 to var
Section: 1.4
Returns: var - 1
Variables: var an integral variable
Abbreviation: --var

Page: 276
Effect: increase var by one
Section: 1.4
Returns: current var value
Variables: var an integral variable
Abbreviation: var++

Page: 276
Effect: decrease var by one
Section: 1.4
Returns: current var value
Variables: var an integral variable
Abbreviation: var--

Name: free store
Definition: Part of memory controlled by the programmer, and not scope nor static/global
Page: 276
Chapter: 11
Section: 2
Variables: var an integral variable
Name2: heap
Abbreviation: var--
Name3: dynamic memory

Instruction: new T (args) / new T {args}
Page: 276
Section: 2
Returns: Pointer to a value, obtained by T (args) / T {args}, and stored in the free store
Variables: T a type
Intuition: values that should survive the creation scope, we don't intend to copy/move, whose memory is dealt manually

Instruction: new T
Page: 276
Section: 2
Returns: Pointer to the free store, with enough space for T
Variables: T a built-in type

Instruction: new T
Page: 276
Section: 2
Returns: Pointer to the free store, initialized with T{}
Variables: T a user-defined type

Instruction: delete var
Page: 276
Effect: if var is not null, call var* destructor, and free the memory it used
Section: 2
Variables: T a type
T* var
Assuming: var is stored on the free space, created by new

Question: What are the issues with free store
Answer: Leaked object
Page: 279
Chapter: 11
Section: 2.1
Variables: T a type
T* var
Answer2: Premature deletion
Answer3: double deletion

Name: Leaked object
Definition: An object that was created with new, and to which there is no reference path to access it anymore
Page: 279
Chapter: 11
Section: 2.1
Intuition: it should have been deleted before the last reference disappear

Name: Premature deletion
Definition: Deleting some memory from free store, and then trying to access it
Page: 279
Chapter: 11
Section: 2.1
Name2: Use after free

Name: Double deletion
Definition: Calling delete twice on a value having a destructor
Page: 279
Chapter: 11
Section: 2.1

Instruction: new T[n]
Page: 279
Section: 2.1
Returns: pointer to free store with enough room for n elements of type T
Kind: T a type

Instruction: delete[] arr
Page: 279
Section: 2.1
Returns: Call the delete operator of the n element of arr, and free the memory in the free store
Kind: T a type
T arr[n]

Question: What is best practice with free store
Answer: Use scoped values with cheap movable type
Page: 280
Chapter: 11
Section: 2.1
Answer2: Uses a handler that deal with the free store and belong in a scoped storage such as vector or smart pointer

Name: A manager object
Definition: An object that contains a pointer to the free store, that is intended to be used as a scoped variable, deal with deleting the free space when going out of scope, being cheap to move
Page: 280
Chapter: 11
Section: 2.1
Example: vector
Example2: smart pointer

Question: What is the space cost of a new allocation
Answer: usually two words, to store the size of the memory allocated
Page: 282
Chapter: 11
Section: 2.1

Instruction: operator new(some_size)
Page: 283
Section: 2.3
Returns: a void* to free space with some_size space available for arbitrary storage
Exception: bad_alloc if there is not enough space available

Instruction: new(args) T
Page: 283
Effect: same as new T, but uses the operator new(size_t, args) instead of default operator
Section: 2.3
Name: Placement syntax

Instruction: new(nothrow) T
Page: 283
Section: 2.4.1
Returns: same as new T, but return nullptr instead of throwing an exception in case of failed allocation

Name: A qualified list
Definition: A initializer_list used for instantiating a new variable with constructor argument
Page: 287
Chapter: 11
Section: 2.4.1

Name: An unqualified list
Definition: A initializer_list which is not qualified
Page: 288
Chapter: 11
Section: 2.4.1

Instruction: {foo_1, ..., foo_n}
Page: 289
Effect: the initializer_list<type_of_foos> whose elements are the foo_i
Section: 2.4.1
Assuming: n>0
all foo's of same type

Page: 299
Implementation: template<class Target, class Source>
Target narrow_cast(Source v)
{
  auto r = static_cast<Target>(v);
  // convert the value to the target type
  if (static_cast<Source>(r)!=v)
    throw runtime_error("narrow_cast<>() failed");
  return r;
}
Section: 5
Chapter: 11
Effect: allow for casting only if it narrow the type but not the value
Name: narrow_cast
Language: C++

Name: casting
Definition: explicit type conversion
Page: 301
Chapter: 11
Section: 5.1
Context: C++

Instruction: reinterpret_cast<T>(expr)
Page: 301
Section: 5.1
Returns: same expr, but its type is T
Intuition: From a void*, or external data

Instruction: dynamic_cast<T>(expr)
Page: 301
Section: 5.1
Returns: if expr is of type T then expr as T, otherwise nullptr

Instruction: const_cast<T>(const T expr)
Page: 301
Section: 5.1
Returns: expr, but of type T only, not const T

Instruction: static_cast<T>(expr)
Page: 301
Section: 5.1
Returns: same as reinterpret_cast<T>( expr), but only valid if the conversion make sens (from base to derived class, between enum/int...)

Instruction: (T) expr
Page: 302
Section: 5.1
Returns: expr, as of type T
Assuming: only if the cast can be done with a combination of
static_cast
reinterpret_cast,
const_cast
Name: C-style cast
Intuition: always avoid. for c compatibility or low level only

Instruction: T(expr)
Page: 302
Section: 5.1
Returns: expr, converted to T
Name: Function-style cast
Intuition: copy constructor considered as a cast