In C++, is it bad to pass a const bool by reference?

前端 未结 6 1652
渐次进展
渐次进展 2021-02-12 23:08

In a practical environment, using gcc or MS Visual Studio, is it bad to pass the value types which are the same size or less than an int by const reference ?

i.e. is it

6条回答
  •  借酒劲吻你
    2021-02-12 23:22

    Although in theory it won't be a good idea, as references are usually implemented using pointers. But nevertheless every reasonable compiler should be smart enough to recognize the indifference in semantics between by-const-reference and by-value for fundamental types.

    Often you don't have a choice if you have some kind of templated interface that has to work for complex types and fundamental types and you don't want excessive specialization overhead (simplest example: std::vector). But if you have a choice, then passing fundamental types by-value should be preferred.

提交回复
热议问题