In C ++, if you have a for loop that "copies" objects of a user-defined type using the move constructor [...]
First of all, the move constructor is used to build the move, which usually means that you don't “copy”: you can implement the move as copy — in fact, the class that is constructive for copying is also constructive — but then why the definition of the move constructor is explicit ?
[...] it doesn't matter if you use ++ i or i ++ as a loop counter?
It depends on what i . If it is a scalar object such as int , then there is no difference.
If i is an iterator of a class type, on the other hand, ++i should be more efficient (on a purely theoretical basis), since operator ++ does not require a copy to be implemented, the iterator must be returned before the iterator is expanded.
Here, for example, is how stdlibC ++ defines the increment operators for the iterator type a std::list :
_Self& operator++() { _M_node = _M_node->_M_next; return *this; } _Self operator++(int) { _Self __tmp = *this; _M_node = _M_node->_M_next; return __tmp; }
As you can see, the postfix version (the one that takes a dummy int ) has more work: you need to create a copy of the original iterator to be returned, then change the internal pointer of the iterator, and then return the copy.
On the other hand, the prefix version simply needs to change the internal pointer and return (the link) to itself.
However, keep in mind that when performing work, all assumptions must be supported by measurement. In this case, I do not expect a reasonable difference between the two functions.
Andy prowl
source share