This is reading notes on Professional C++ by Marc Gregoire, but also includes other notes.

  1. In switch statement, expression must be integer type.
    1. Why can’t be string? Because C++ does not recognize string as a type. Ref:
      1. stackoverflow.com
      2. llvm.org
      3. hbfs.wordpress.com
  2. Array initialization
    1. in C style:
      1. int myArray[3] = {0}; // all 0
      2. int myArray[3] = {1}; // first 1, other two 0
      3. int myArray[] = {0,1,2}; // an array of 3 elements, [0;1;2]
    2. in C++ style:
      1. array<int, 3> myArray = {0, 1, 2}
    3. For array of variable length, use vector
  3. Range-based for loop
    1. for (int i : myArray)
  4. __func__ is implicitly declared (i.e. can be used as function name directly inside of a function) as:
    1. static const char __func__[] = “func-name”;
  5. Alternative function syntax (with trailing return type):
    1. auto func(int i) -> int // {…}
  6. Function return type deduction (use compiler to figure out return type)
    1. auto func(int i) // {…}
    2. all returns should resolve to the same type
    3. in recursive calls, first return must be non-recursive
  7. Type inference
    1. by auto:
      1. auto result = func();
    2. by decltype:
      1. int x = 1; decltype(x) y = 2;
  8. const int i = 1 is not equal to #define i 1
  9. int *j; later *j is called “dereference”
  10. C++ coding style: https://google.github.io/styleguide/cppguide.html
  11. Better only use static_cast (according to Ira Pohl)
  12. binary literal to string:
    std::string str = std::bitset<8>(123).to_string();</code>
  13. measure execution time of function:
    https://stackoverflow.com/questions/22387586/measuring-execution-time-of-a-function-in-c