Uncover the veil of the C ++ compiler and you will find that references are usually implemented in the form of pointers, so passing through references usually means actually passing a pointer. Therefore, if you pass an object of built-in data type (such as INT), passing the value is more efficient than passing the reference. Therefore, for built-in data types, it is a better choice to transfer values when you are hovering between passing values and passing constant references. This is true for the function objects in the iterator and STL, because they are designed to be more suitable for passing values. This is the practice of C ++. It is the responsibility of the person implementing the iterator and function objects to consider the efficiency and truncation issues during replication. (This is a "which rule to use depends on which c ++ is used currently". For details, see article 1st)
The size of built-in data types is small, so some people come to the conclusion that all smaller data types are suitable for passing values, even if they are user-defined. This is an unreliable reasoning. The cost of calling the copy constructor cannot be determined by the small size of an object. Many objects-including most STL containers-contain only one pointer and a small amount of other content, but when copying such an object, all the content it points to needs to be copied. This will be very expensive.
Even if the copy constructor of a smaller object does not incur expensive costs, it will introduce performance problems. Some compilers treat internal data types and user-defined data types separately, even if their original representation is identical. For example, some compilers are happy to put a single double value into a register. This is a general language, but when an object containing only one double value is put into a register, the compiler will report an error. When you encounter such a problem, you can use references to pass such objects, because the compiler will put the pointer (the specific implementation of the reference) into the register at this time.
The small user-defined data type does not apply to the value transfer method. Another reason is that, as user-defined data types, their sizes are not fixed. Nowadays, small types may become very large in future versions, because their internal implementation methods may change. Even if you change the implementation of the c ++ language, the type may be affected. For example, when I compile the above example, some implementations of the string in the standard library have actually reached seven times the size of others.
Generally, only the built-in data types, STL iterators, and function object types are suitable for value passing. For all other types, we recommend that you use reference constants instead of passing values.
Keep in mind
- Use reference constants instead of passing values. Because it is more efficient to transmit references and can avoid "truncation ".
- This rule is not applicable to built-in data types, STL iterations, and function object types. Generally, the value passing method is more practical for them.