References, const, and function boundaries
Choose parameter forms that communicate copying, borrowing, and mutation.
What you’ll learn
- Distinguish value and reference parameters
- Explain a const access contract
- Recognize lifetime requirements for borrowed access
A function boundary communicates intent
The way a function accepts a value tells readers something about its relationship with the caller. A value parameter creates a local object initialized from the argument. A reference parameter refers to another object rather than providing a separate independent object. This is not only a performance choice: it also influences whether changes can affect the caller. Choose the form that matches the operation's responsibility instead of applying one rule to every possible type.
Use values for small independent inputs
For a small count or flag, passing by value is usually straightforward. The function receives its own parameter value and can manipulate that local value without changing the caller's variable. A value can also be an appropriate way to express that a function needs an owned copy of a larger object. The cost depends on the type and on copying or moving behavior, so avoid claiming that every value parameter is expensive or that every reference is free.
Borrow for inspection or modification
A non-const reference can expose an object for modification, so callers should be able to understand that possibility from the contract. A const reference provides read-only access through that reference and can avoid an unnecessary copy for a larger input. Const does not automatically make every related object globally immutable, and other references may still modify shared state. Describe what the function is allowed to do and do not promise thread safety merely because one parameter is const.
Keep the referenced object alive
A borrowed reference is useful only while the referred object exists. Returning a reference to a local automatic object is invalid because the local object is destroyed when the function returns. Similarly, keeping a reference into a container can become unsafe when an operation invalidates it. Review the lifetime and invalidation rules of the specific type. A short function call that only inspects an existing object has different requirements from storing a reference for later use.
Review an example contract
Suppose describe_book produces a formatted description but should not change the book. A const reference may be suitable for an existing larger book object, while a returned string provides an owned result to the caller. In contrast, rename_book intentionally changes a book and should expose that effect clearly. Test that inspection leaves observable state unchanged and that mutation preserves the book's invariants. Let the interface communicate these differences before trying to optimize implementation details.
Try it yourself
Choose a parameter approach for a function that checks a small integer and one that reads a large existing record without changing it.
Show a possible solution
A small integer can normally be passed by value. A const reference may be suitable for the existing larger record when the function only borrows it during the call. State the lifetime and mutation contract rather than treating this as an absolute performance rule.
Check your understanding
Try 14 questions across modern c++ foundations. Read each explanation and revisit anything that needs another pass.
Practise this subjectGo to the source
Use these primary references to check details and explore the topic further.
Saved in this browser. Signed-in progress syncs to your account.