Thursday 12 September 2019

c++ - Google's style guide about input/output parameters as pointers

The point they are making (which I disagree with) is that say I have some function


void foo(int a, Bar* b);

If the b argument is optional, or it is unnecessary sometimes, you can call the function like so


foo(5, nullptr);

If the function was declared as


void foo(int a, Bar& b);

Then there is no way to not pass in a Bar.


This point (emphasis mine) is completely opinion-based and up to the developer's discretion.



In fact it is a very strong convention in Google code that input arguments are values or const references while output arguments are pointers.



If I intend for b to be an output parameter, either of the following are perfectly valid and reasonable.


void foo(int a, Bar* b);  // The version Google suggests
void foo(int a, Bar& b); // Reference version, also perfectly fine.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...