When passing parameters to functions and methods that will be called millions of times in a short amount of time, the overhead of passing said parameters begins to show.
void foo(const SomeType& st) { ... }
For types like std::string, std::vector etc... the rule is to pass by reference so that pointless copies don't occur. However when dealing with PODs such as doubles, ints etc, the story is quite different.
With regards to performance, If the function/method does not need to mutate the parameters, what are the common 'things to look out for' when deciding if one should pass by reference, const reference or copy?
void foo1(SomeType& st)
{
...
}
void foo2(const SomeType& st)
{
...
}
void foo3(SomeType st)
{
...
}
void foo4(SomeType* st)
{
...
}
- Should an int/unsigned int always be passed by copy?
- Should a double be passed by copy only on 64-bit or higher targets?
- If the function can be inlined - does how the parameters are passed in matter?
- How does aliasing affect problems when passing in by ref/const ref?
- Under what circumstance would a explicit copy of the parameter be beneficial on the stack?
Note: This is a not a question about const-correctness. Also looking for answers relating to gcc and msvc on 32/64-bit platforms.
Some possibly related Q/As:
https://stackoverflow.com/a/2139254/754951