cppgraphqlgen
cppgraphqlgen copied to clipboard
Pass smart pointers by value
We have gotten some code smells reported by SonarQube regarding these smart pointer rvalue references, so I thought I could maybe contribute and suggest passing these smart pointers by value instead.
Maybe there is a reason they should be rvalue references?
This is the kind of code smell we got: https://rules.sonarsource.com/cpp/tag/confusing/RSPEC-5954
The reason I favored r-value references is that we have another static analyzer rule internally which warns against passing values larger than 4 * (sizeof(void*))
. The explanation is that this generates more code at the call sites to perform a copy, whereas passing by a reference puts all of the copy/move code inside the function/method you're calling.
I think the threshold for that rule's trigger (4 * sizeof(void*)
) is probably a heuristic. I've also heard a rule of thumb that you should pass by reference for anything greater than 2 * sizeof(void*)
to maximize the number of parameters that will fit in registers instead of the stack.
Right and passing by value and moving is not as optimal as passing rvalues references?
Just wondering as many example codes for passing smart pointers in constructors are using the pass by value and move method. Example