TIL-3: final, Small Integer operations, vector
final
- final is a way to forbid you overriding a virtual function again
- It should not be used with
override
as it implies override
- It should not be used with
- final allows compilers to do optimization1
Base virtual destructor
- Base virtual destructor implies children virtual destructor, so we can use rule of 0 here.
Small Integer Operations 2
- When doing arithmetic operations on small unsigned integers (those with rank less than
int
), they will be promoted to int 3- 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
- the deducted return type with
- 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
Weird vector bool 4
- 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 toauto &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
- To store the proxy object, you need to use
-
Compilers are allowed to call function directly instead of following vtable. See this answer for more details. ↩
-
See Jason Turner’s video C++ Weekly - Ep 310 - Your Small Integer Operations Are Broken! for more details. ↩
-
cppreference provides a detailed explanation about arithmetic conversions and integer promotions ↩
-
See Jason Turner’s video C++ Weekly - Ep 325 - Why vector of bool is Weird for more details. ↩