Pass by value, const value, reference, const reference, pointer, const pointer

Find out more and find the answer to determine how to get through the old post (sorry for the duplicate)

  • If the function intends to change the argument as a side effect, take this non-constant reference.
  • If the function does not change its argument and the argument of a primitive type, take it by value.
  • Otherwise, take it from the const link, except for the following cases
    • If then the function would have to make a copy of the const link anyway, take it by value.

[Original post below]

I would like to generalize the use of transmission by value, constant, link, constant link, pointer, constant pointer and please correct me and give me your suggestions.

  • As for the link and the pointer, use const if possible (thanks everyone).
  • There is no performance difference between passing by reference and pointer.
  • If the size does not exceed the pointer (thanks to Mark Ransom), scroll to the value.

And some questions:

  • I rarely see a transition by const value. Is this useful, or will the compiler detect a constant when passing by value?
  • The const link takes up too much space. Can I just use pass by value? Will modern compilers be able to optimize it so as not to sacrifice performance?

In accordance with the article "Want Speed? Pass by Value" juanchopanza is mentioned, I add one more element.

  • If you copy your arguments, pass them by value and let the compiler do a different copy from passing them via the const link and making a copy yourself in the body of the function.

Thanks a lot!

+4
source share
1 answer

I rarely see a transition by const value. Is this useful, or will the compiler detect a constant when passing by value?

Passing const values ​​does not really exist. When you go through a value, you cannot change the value so that the changes are visible outside the subroutine. This is because when you pass by value, a copy is made from the original value and this copy is used in the function.

The const reference takes up too much space. Can I just skip past the cost? Can modern compilers optimize it so as not to sacrifice performance?

Passing by ( const ) link does not match passing by value. When you pass by reference, the value is NOT copied, the memory location is simply provided, and you can "change" (indirectly) the value that you pass by reference.

Take for example the following:

 void byValue(int x) { x += 1 } void byRef(int &x) { x += 1 } // ... { y = 10; byValue(y); cout << y << endl // Prints 10 byRef(y); cout << y << endl; // Prints 11 } // ... 

Use the constant as much as possible.

Passing const , where necessary, is always a good idea. This helps the readability of the code, allows others to know what will happen to the values ​​that they pass to the method, and helps the compiler catch any errors that may occur when the value changes inside the method.

There is no performance difference between passing by reference and pointer.

Negligible amount, if any. The compiler will take care of the details here. This saves you the effort of creating a pointer, and it beautifully plays it out for you.

If the size does not exceed a word, go to the value.

As Mark points out, you do this if the value is less than a pointer. Pointers have different sizes on 32-bit and 64-bit systems (hence the name), and therefore this is really at your discretion. I am a fan of missing pointers for almost everyone except primitive types ( char , int8_t , int16_t , float , etc.), but this is just my opinion.

+4
source

All Articles