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

前端 未结 4 2288
终归单人心
终归单人心 2021-02-19 10:41

The Google C++ Style Guide draws a clear distinction (strictly followed by cpplint.py) between input parameters(→ const ref, value) and input-output or output parameters (→ non

4条回答
  •  逝去的感伤
    2021-02-19 11:33

    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.
    

提交回复
热议问题