google / styleguide

Style guides for Google-originated open-source projects
https://google.github.io/styleguide/
Apache License 2.0
37.32k stars 13.3k forks source link

Why output parameters be sugguested as reference rather than pointer #803

Closed yexinhua1998 closed 3 months ago

yexinhua1998 commented 8 months ago

Google c++ style sugguest output parameter as pointer before. But after this commit, this suggestion is changed and output parameters is suggested as reference. I am confused about why do this chagne and what problem you meet in practice and what benefit with new rule. Thanks for answer!

vapier commented 3 months ago

i don't have the references off the top of my head, but iirc, this is to move away from raw pointers and to things like std::optional (new in C++17) to mitigate common errors like dereferencing NULL pointers.

considering how long the pointer guidance has been around, the style guide is taking a bit of a transitional approach. you can see how it talked about absl::optional at the time, but has since been updated to only mention std::optional.