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

前端 未结 4 2289
终归单人心
终归单人心 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:20

    The reason for demanding that output parameters are passed as pointers is simple:

    It makes it clear at the call site that the argument is potentially going to be mutated:

    foo(x, y);     // x and y won't be mutated
    bar(x, &y);    // y may be mutated
    

    When a code base evolves and undergoes incremental changes that are reviewed by people who may not know the entire context all the time, it is important to be able to understand the context and impact of a change as quickly as possible. So with this style rule, it is immediately clear whether a change introduces a mutation.

    0 讨论(0)
  • 2021-02-19 11:26

    They likely use it for consistency because they use output parameters both as references to existing memory (they're modifying previously initialized variables) and as actual outputs (the output arguments are assumed to be assigned by the function itself). For consistency, they use it as a way to more clearly indicate inputs vs. outputs.

    If you never need a function/method to assign the memory of the output parameter, like returning a pointer from a lookup or allocating memory itself and returning it through a pointer, use references. If you need to do that but don't care about using pointers to act as an indication of whether a parameter is input or output, use references for output parameters when appropriate. There's no absolute requirement to use pointers in all cases unless the requirements of that function/method itself requires it.

    0 讨论(0)
  • 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.
    
    0 讨论(0)
  • 2021-02-19 11:40

    You're first question: "So, what's the point to always demand a pointer if I want to avoid the pointer to be null?"

    Using a pointer announces to the caller that their variable may be modified. If I am calling foo(bar), is bar going to be modified? If I am calling foo(&bar) it's clear that the value of bar may be modified.
    There are many examples of functions which take in a null indicating an optional output parameter (off the top of my head time is a good example.)

    Your second question: "Why only use references for input arguments?"

    Working with a reference parameter is easier than working with a pointer argument.

    int foo(const int* input){
        int return = *input;
    
        while(*input < 100){
            return *= *input;
            (*input)++;
        }
    }
    

    This code rewritten with a reference looks like:

    int foo(const int& input){
        int return = input;
    
        while(input < 100){
            return *= input;
            input++;
        }
    }
    

    You can see that using a const int& input simplifies the code.

    0 讨论(0)
提交回复
热议问题