Why do some people prefer “T const&” over “const T&”?

后端 未结 9 1618
醉梦人生
醉梦人生 2020-12-02 12:25

So, I realize that const T& and T const& are identical and both mean a reference to a const T. In both cases, the reference is also constan

9条回答
  •  一向
    一向 (楼主)
    2020-12-02 12:35

    This will make a difference when you have more then one const/volatile modifiers. Then putting it to the left of the type is still valid but will break the consistency of the whole declaratiion. For example:

    T const * const *p;
    

    means that p is a pointer to const pointer to const T and you consistenly read from right to left.

    const T * const *p;
    

    means the same but the consistency is lost and you have to remember that leftmost const/volatile is bound to T alone and not T *.

提交回复
热议问题