final

  • final is a way to forbid you overriding a virtual function again
    • It should not be used with override as it implies override
  • final allows compilers to do optimization[1]

Base virtual destructor

  1. Base virtual destructor implies children virtual destructor, so we can use rule of 0 here.

Small Integer Operations

  • When doing arithmetic operations on small unsigned integers (those with rank less than int), they will be promoted to int[2]
    • the deducted return type with auto will become int
    • any further operations on this value (e.g. right shift) are operating on int, which can yield unexpected impact
  • To solve this problem, when doing arithmetic operations on small unsigned integer, we need to use static_cast to change their type back to the original unsigned type

See Jason Turner’s video C++ Weekly - Ep 310 - Your Small Integer Operations Are Broken! for more details.

Weird vector bool

  • C++ standard “recommends” a space-optimized specialization of std::vector<bool>
    • This optimization works by actually placing bool inside a bit
  • However, if we need to access the member inside the list, the operator[] returns a proxy object for us
    • that allows us to view/change the value
  • But the proxy project cannot be bound to lvalue reference auto & while other instantiation of vector all allow you to auto &ref = v[idx]
    • To store the proxy object, you need to use auto sth = v[idx]
    • If you want to use bit field by yourself, you cannot bind the member by reference either

See Jason Turner’s video C++ Weekly - Ep 325 - Why vector of bool is Weird for more details.

  1. Compilers are allowed to call function directly instead of following vtable. See this answer for more details. ↩

  2. cppreference provides a detailed explanation about arithmetic conversions and integer promotions ↩