C++ style: Stroustrup' s placement of pointer asterisks

后端 未结 3 425
鱼传尺愫
鱼传尺愫 2021-02-05 02:09

Does anyone know why Stroustrup\'s style is the placement of pointers as follows? Specifically, what Stroustrup has provided for guidance about this matter?

int         


        
相关标签:
3条回答
  • 2021-02-05 02:34

    C++ emphasis heavily on types and when it comes to pointers declaration, to avoid any sort of confusion, Bjarne suggested - Stick to one pointer per declaration.

    From Bjarne Stroustrup's C++ Style and Technique FAQ [emphasis added]:

    Is int* p; right or is int *p; right?

    Both are "right" in the sense that both are valid C and C++ and both have exactly the same meaning. As far as the language definitions and the compilers are concerned we could just as well say int*p; or int * p;

    The choice between int* p; and int *p; is not about right and wrong, but about style and emphasis. C emphasized expressions; declarations were often considered little more than a necessary evil. C++, on the other hand, has a heavy emphasis on types.

    A typical C programmer writes int *p; and explains it *p is what is the int emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.

    A typical C++ programmer writes int* p; and explains it p is a pointer to an int emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.

    The critical confusion comes (only) when people try to declare several pointers with a single declaration:

    int* p, p1; // probable error: p1 is not an int*

    Placing the * closer to the name does not make this kind of error significantly less likely.

    int *p, p1; // probable error?

    Declaring one name per declaration minimizes the problem - in particular when we initialize the variables. People are far less likely to write:

    int* p = &i; int p1 = p; // error: int initialized by int*

    And if they do, the compiler will complain.
    Whenever something can be done in two ways, someone will be confused. Whenever something is a matter of taste, discussions can drag on forever. Stick to one pointer per declaration and always initialize variables and the source of confusion disappears.

    See The Design and Evolution of C++ for a longer discussion of the C declaration syntax.

    0 讨论(0)
  • 2021-02-05 02:49

    I believe stroustrup's style implies that variable declarations like those should be avoided. This also seems to be the general consensus among the community.

    0 讨论(0)
  • 2021-02-05 02:56

    I cannot speak for Bjarne, but tying the asterisk (and ampersand in case of reference) to the type makes sense because being a pointer is semantically part of the type of the variable. The name of the variable is p and its type is int*. The name is not *p and the type is not int.

    It is nearly always possible to avoid multiple variable declarations in a single declaration, so that is not an issue.

    In my opinion, this approach is clearer, especially in case of return types:

    T*
    function(Args...);
    
    T
    *function(Args...);
    
    0 讨论(0)
提交回复
热议问题