What are the benefits to passing integral types by const ref

前端 未结 12 1793
情话喂你
情话喂你 2021-02-05 22:18

The question: Is there benefit to passing an integral type by const reference as opposed to simply by value.

ie.

void foo(const int& n); // case #1
<         


        
12条回答
  •  醉梦人生
    2021-02-05 22:54

    well the cost of a reference is the same typically of that of an integral type, but with the reference you have an indirection that has to take place, because the reference to some memory has to be resolved into a value.

    Just copy by value, stick to an immutable convention for built-in types.

提交回复
热议问题