Skip to content

0x342 C++

Will mainly follow Bjarne Stroustrup's book's structure

1. Abstraction

1.1. Construction, Cleanup, Copy and Move

class X {
    X(Sometype); // ‘‘ordinary constructor’’: create an object
    X(); // default constructor
    X(const X&); // copy constructor
    X(X&&); // move constructor
    X& operator=(const X&); // copy assignment: clean up target and copy
    X& operator=(X&&); // move assignment: clean up target and move
    ˜X(); // destructor: clean up

Destructor is invoked implicitly upon scope exit or by delete. Destructor can also be virtual since an object can be manipulated through its base interface and get deleted through that interface. A virtual destructor make sure that the derived class destructor get called.

2. Standard Library

2.1. IO

2.1.1. iostream

the only way to pass or return stream is by non-const reference

  • stream cannot be copyied or assigned
  • stream might change state (so non-const)
istream& read(istream& is, int hogehoge);

each stream has a iostate, which indicates the state of the current stream, it can be

  • good: valid state
  • eof: get eof
  • fail: recoverable failure (e.g: cin string to a int type)
  • bad: stream is corrupt somehow

The state can be modified by users

When it is in good state, the stream is evaled as true, for example, to continuously read until the eof

string line, word;

while(getline(cin, line)){

while(cin >> word){

2.1.2. fstream

2.1.3. stringstream

2.2. Concurrency

std::thread: it needs to join or detach. use RAII to make sure it will be handled correctly. std::mutex: RAII with std::lock_guard or std::unique_lock(can lock multiple mutex and unlock) std::recursive_mutex: allow a thread to acquire a mutex multiple times without deadlock

2.3. Algorithm

sort: looks optimized using different sort (insertion <-> quick) when length is short or long Standardization

3. STL Container

not thread-safe

3.1. Sequential Container

3.2. Associative Container

4. C++ Standards

4.1. C++11 (C++0x)

lambda expression

4.2. C++14

4.3. C++17

4.4. C++20

Generic Programming:

concept, constraint

4.5. C++23 ?!

5. Reference

[1] Bjarne Stroustrup's C++ Programming Language 3rd edition

[2] llvm libc++ implementation

[3] A History of C++ by Bjarne Stroustrup

[4] C++ standard

[5] C++ Primer