A reference (&) is an alias. Once initialized, it is the variable it refers to.
- Difference from Pointers: Pointers can be
nullptror reassigned to point elsewhere. References must be initialized and cannot be changed to refer to something else. - HPC Context: We use
const T&(const reference) to pass large structs to functions. This avoids copying the entire object (saving cache and cycles) while promising not to modify it (const).
Pass-by-Value vs. Pass-by-Reference:
void func(std::vector<int> v): Copies the entire vector (Slow).void func(std::vector<int>& v): Passes the address (Fast).