c++ - Difference in passing const &object vs. const object -


suppose have function one

bool verifyobject(const myobj& obj); 

or one

bool verifyobject(const myobj obj); 

as far understand, when pass in second case, copy made. however, compiler might exploit constness pass in first case. such, see no difference in passing const object in either way.

am wrong?

as far understand, when pass in second case, copy made.

yes; although in cases (such when passing temporary) copy elided.

however, compiler might exploit constness pass in first case.

no; unless argument temporary, not 1 of situations in copy can elided. when calling function, compiler may not know parameter const within function, since top-level const can omitted when declaring function. if can determine passing reference equivalent passing copy (which won't case if copy-constructor or destructor have observable side effects, or if class has mutable members), can't change calling convention since callers have no way of knowing change.

having said that, if function inline, or compiler whole-program optimisation, optimisations such possible; if don't change program's behaviour.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -