C++ Pass By Reference Guidelines

Currently I am optimizing my methods and I wonder which parameters should be made a const reference by default, instead of pass by value.

I understand that making a simple datatype such as int, enum or string a const reference can actually lead to less performance and higher memory usage.

Even if the effect is barely noticable I want to get this right in my methods as a professional. So looking at the basic unreal types: FString, FVector variants etc. does it make sense to make these const & by default instead of a pass by value parameter? Do we always pass structs as a const reference? Is there a single situation where passing a pointer as a const reference would have any benefits?

Iā€™d like to make a definitive guide for this.

Performance wise, anything that is not a basic type, including pointer, would perform faster passed as reference.

1 Like