3 ms·
Passing by value can be fine for small size structs, but it is not the common practice. I expected to see any of this function to use const reference. For gen
by random9e7de4c1 5y ago
Passing by value can be fine for small size structs, but it is not the common practice.
I expected to see any of this function to use const reference.
For general guidelines on parameters passing (and other guideilines), the C++ core guidelines are pretty useful for those who want to learn:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-conventional https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines...
- spacechild1 5y ago> Passing by value can be fine for small size structs, but it is not the common practice. It can also be fine with larger objects (with heap memory) if you plan to make a copy anyway, because you can just use std::move. The advantage is that the function works the same with lvalue and rvalue references, so you don't have to write the same function twice. An alternative is to use "universal references" (T&& where T is a template parameter) + std::forward. This is often seen in constructors. But yeah, usually you would just use const references. BTW, instead of 'const std::string&' it is even better to use a std::string_view because it works both with std::string and C strings - without creating hidden copies for the latter. If you pass a C string as a 'const std::string&' argument, you actually create a new temporary std::string, whereas std::string_view will just store the pointer and length.