Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move constructor and pre-increment vs post-increment

In C++, if you have a for loop that "copies" objects of a user defined type using a move constructor, does it make any difference if you use ++i or i++ as the loop counter?

I know this question seems rather vague, but I was (I believe) asked this in a phone interview. I wasn't sure if I understood the question correctly, and the interviewer took this as my not knowing the answer, and cut the interview short.

What could he have been getting at?

like image 961
Scott Avatar asked May 09 '13 16:05

Scott


People also ask

Which operation is faster i ++ or ++ i?

Though we can say that the ++i is slightly faster than i++. The i++ takes local copy of the value of i before incrementing, while ++i never does. Sometimes some compiler optimizes the code if possible.

Which one is better pre-increment or post increment?

Pre-increment is faster than post-increment because post increment keeps a copy of previous (existing) value and adds 1 in the existing value while pre-increment is simply adds 1 without keeping the existing value.

What is ++ i and i ++ in C?

In C, ++ and -- operators are called increment and decrement operators. They are unary operators needing only one operand. Hence ++ as well as -- operator can appear before or after the operand with same effect. That means both i++ and ++i will be equivalent.

What is difference between post increment and pre-increment in for loop?

1) Post-Increment (i++): we use i++ in our statement if we want to use the current value, and then we want to increment the value of i by 1. 2) Pre-Increment(++i): We use ++i in our statement if we want to increment the value of i by 1 and then use it in our statement.


1 Answers

In C++, if you have a for loop that "copies" objects of a user defined type using a move constructor [...]

First of all, a move constructor is used for move-constructing, which usually means you are not "copying": you can realize moving as copying - in fact, a class which is copy-constructible is also move-constructible - but then why defining a move constructor explicitly?

[...] does it make any difference if you use ++i or i++ as the loop counter?

It depends on what i is. If it is a scalar object, like an int, then there is no difference at all.

If i is a class-type iterator, on the other hand, ++i should be more efficient (on a purely theoretical ground), because the implementation of operator ++ will not have to create a copy of the iterator to be returned before the iterator itself is incremented.

Here, for instance, is how stdlibc++ defines the increment operators for the iterator type of an 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 accepting a dummy int) has more work to do: it needs to create a copy of the original iterator to be refurned, then alter the iterator's internal pointer, then return the copy.

On the other hand, the prefix version just has to alter the internal pointer and return (a reference to) itself.

However, please keep in mind that when performance is concerned, all assumptions have to be backed up by measurement. In this case, I do not expect any sensible difference between these two functions.

like image 52
Andy Prowl Avatar answered Nov 09 '22 11:11

Andy Prowl