google / google/styleguide

Could somebody elaborate 'use a const pointer when the non-optional form would have used a reference'?

Open
#639 2 comments 1 reaction 0 assignees View on GitHub
lang:cpp question
Dominant language
HTML
Stars
39.6k
Forks
12.9k
Avg merge
42m
Merged PRs (30d)
15

Description

in cppguide.html,
Could somebody please elaborate 'use a const pointer when the non-optional form would have used a reference'? It will be also helpful if some examples can be added.

My understanding is as follows,
* Non-optional input parameters - values or const references
* Non-optional output and input/output parameters - references
* Optional input parameters(by value) - absl::optional
* the non-optional form would have used a reference(**<= This means Optional input parameters by references?**) - a const pointer
* Optional outputs and optional input/output parameters - non-const pointers

Thanks in advance.

> Parameters are either inputs to the function, outputs from the function, or both. Non-optional input parameters should usually be values or const references, while non-optional output and input/output parameters should usually be references (which cannot be null). Generally, use absl::optional to represent optional by-value inputs, and use a const pointer when the non-optional form would have used a reference. Use non-const pointers to represent optional outputs and optional input/output parameters.

Contributor guide

Open the contributing guide

Research direction

Start in cppguide.html by reading the surrounding parameter guidance and the quoted rule about const pointers. Clarify when an optional input should use a const pointer rather than a reference, and add examples that distinguish the listed parameter cases. Done means the wording and examples make the rule unambiguous.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.