I've been studying the Bitcoin Core code, and I noticed that it uses a template READWRITE to serialize data structures for storing blocks. But it never seems to actually call "READWRITE(nTxs)", the VARINT (CompactSize) that encodes the number of transactions. It reads the transactions to a vector (primitives/block.h), and it almost seems like it just magically knows how many txs there are. What gives?
The transactions are passed in as a vector. Bitcoin Core's serialization framework understands that all vectors should be serialized with a compact sized unsigned integer preceding the serialization of the data within the vector itself.