0

I know, the question of passing scalars by value vs. by reference has been asked and answered gazillion times before, and I know - in general - they should be passed by value. But what about inline functions? And the reason I ask this is I just did some refactoring with Visual Studio 2022, and when I called VS's "Extract Function" command, it extracted the scalar variables like constant reference (e.g. const int &).

Does VS know something I don't in relation to inline functions, or should I double check and override the code generated by VS?

Edit #1: I also noticed, when pointers were passed as parameters, it didn't use the inline keyword. Is it because it knows these functions cannot be inlined? If yes, why?

Edit #2: It also did some other funny things. For example: (Vehicle *& vehicle_ptr) It just referenced a pointer. I think it proves that VS does the function extraction using some very simple script.

10
  • It probably just always uses references regardless of the type of the argument Commented Feb 22, 2022 at 18:24
  • 1
    FWIW, even if you use inline, there is no guarantee that the function will be inlined, so pass the parameters as you would normally. Commented Feb 22, 2022 at 18:25
  • "Does VS know something I don't in relation to inline functions" Unlikely. Commented Feb 22, 2022 at 18:26
  • 5
    VS's Extract Function is following a cookie cutter script. It's generically the right thing to do in many cases, but not necessarily in this particular case. The script could have been made more complicated by checking for this particular case, but maybe it was a Friday and the dev who was working on it was late for happy hour. Commented Feb 22, 2022 at 18:32
  • 4
    inline makes no difference on wether you should pass by value or pass by reference. You can use the information you found in gazillion other places. I suggest isocpp.github.io/CppCoreGuidelines/… because it is rather exhaustive. Note that it does not mention inline because it makes no difference Commented Feb 22, 2022 at 18:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.