Pergunta

i read the C++ primer 5th. i saw that it is ok when we use non-reference parameters in assignment operators that use copy and swap,but in the other assignment operators we always use reference parameters and copy the right-hand operand before destroying left-hand operand to make sure the assignment operators work correctly if an object is assigned to itself. why not use non-reference parameter in the assignment operators that will copy in the function body later so that we need not copy the right-hand operand in the function body?

Foi útil?

Solução

Copy assignment operators usually should take their parameter by value (not reference) in order to do copy-and-swap using the by-value parameter as the copy.

If you're not doing (just) copy-and-swap then perhaps your copy assignment operator should take its parameter by reference. As for why you would not do copy-and-swap: probably because of some specific optimization for the type. Consider for example a std::array of a trivially copyable type (trivial to save ourselves the trouble of talking about exception safety). The most efficient possible assignment is to directly copy the bytes: you don't want a temporary copy of the object and you certainly don't want a swap. So in practice you'd take your parameter by reference, write std::copy, and expect the compiler to do it optimally.

Special-casing self-assignment in a copy assignment operator is sometimes seen in textbooks even with copy-and-swap, but was generally a bad idea in C++03. Although it optimizes the case of self-assignment, the by-reference parameter de-optimizes the case of assignment from a temporary. In C++11 it's less obviously bad, because the case of assignment from a temporary can be dealt with by the move assignment operator instead of the copy assignment operator. But it's probably still unnecessary. Self-assignment is pretty rare in real code, so it can even turn out that the pointer comparison in the common case adds up to more runtime than the copy in the rare case.

I haven't read the 5th Edition of "C++ Primer", and you have chosen not to show the code you're asking about, so I can't comment on the specific cases in it where it gives assignment operators (copy assignment or otherwise) that take parameters by reference.

Sorry, but on balance I think it's statistically more likely you are confused than the book is ;-) You say:

... in assignment operators that use copy and swap,but in the other assignment operators

OK, so "other assignment operators" is assignment operators that don't use copy-and-swap?

we always use reference parameters and copy the right-hand operand before destroying left-hand operand

If you copy the right-hand operand then you are doing copy-and-swap. Or at least you're very close to it, because having made a copy there's little point using anything other than swap to get the copy into the left-hand side. Either way, if the first thing a function does is to copy a by-reference parameter, then it should have taken the parameter by value.

to make sure the assignment operators work correctly if an object is assigned to itself

If your technique for ensuring that self-assignment works correctly is to always copy the right-hand operand, then you should take the parameter by value. Nothing else is needed. However, there are other techniques for ensuring self-assignment works. It's possible one of those is happening in the code you don't show.

Outras dicas

The prime reason is efficiency. For a small class / struct, it probably doesnt matter, but if it is a large complex class, that is a different matter. If you pass-by-value, you are going to invoke a potentially large and complex copy operation. This may require a large memory allocation perhaps.

The real answer is that sometimes it doesn't matter. Often it really matters, and so the best approach is to just always use a reference.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top