I originally assumed that it is bad practice to move an l-value reference parameter. Is this indeed commonly agreed by the C++ developer community?
When I call a function that has an R-value reference parameter, it's clear that I must expect that the passed object can be moved. For a function that has an L-value reference parameter, this is not so obvious (and before move semantics were introduced with C++11, it wasn't possible at all).
However, some other developers I recently talked to don't agree that moving l-value references shall be avoided. Are there strong arguments against it? Or is my opinion wrong?
Since I was asked to provide a code example, here is one (see below). It's an artificial example just for demonstrating the issue. Obviously, after calling modifyCounter2(), a call of getValue() will cause a segmentation fault. However, if I were a user of getValue() without knowing its internal implementation, I would be very surprised. If, on the other hand, the parameter were an R-value reference, I would be totally clear that I should not use the object anymore after calling modifyCounter2().
class Counter
{
public:
Counter() : value(new int32_t(0))
{
std::cout << "Counter()" << std::endl;
}
Counter(const Counter & other) : value(new int32_t(0))
{
std::cout << "Counter(const A &)" << std::endl;
*value = *other.value;
}
Counter(Counter && other)
{
std::cout << "Counter(Counter &&)" << std::endl;
value = other.value;
other.value = nullptr;
}
~Counter()
{
std::cout << "~Counter()" << std::endl;
if (value)
{
delete value;
}
}
Counter & operator=(Counter const & other)
{
std::cout << "operator=(const Counter &)" << std::endl;
*value = *other.value;
return *this;
}
Counter & operator=(Counter && other)
{
std::cout << "operator=(Counter &&)" << std::endl;
value = other.value;
other.value = nullptr;
return *this;
}
int32_t getValue()
{
return *value;
}
void setValue(int32_t newValue)
{
*value = newValue;
}
void increment()
{
(*value)++;
}
void decrement()
{
(*value)--;
}
private:
int32_t* value; // of course this could be implemented without a pointer, just for demonstration purposes!
};
void modifyCounter1(Counter & counter)
{
counter.increment();
counter.increment();
counter.increment();
counter.decrement();
}
void modifyCounter2(Counter & counter)
{
Counter newCounter = std::move(counter);
}
int main(int argc, char ** argv)
{
auto counter = Counter();
std::cout << "value: " << counter.getValue() << std::endl;
modifyCounter1(counter); // no surprises
std::cout << "value: " << counter.getValue() << std::endl;
modifyCounter2(counter); // surprise, surprise!
std::cout << "value: " << counter.getValue() << std::endl;
return 0;
}
const
). – Bataanvoid foo(const std::string& arg) { bar( std::move(arg) ) }
will call a copy constructor and move a temporary object, this is generally legal but have no sense, because it negate the copy elision. – Soulsearching~Counter()
and as well as to thedelete value;
– SoulsearchingCounter(Counter && other)
wasvalue = other.value; other.value = new int;
instead, thenmodifyCounter2
is like writingvoid Counter::reset()
– Cryptogramfront
on the vector afterwards. – AdhernCounter
is wrong, not really forgetValue
(as state above,vector::front
cannot be called from emptyvector
), but you no longer can do thing to reenablecounter
, such ascounter = anothercounter
. – Breljevector.clear()
, even in that case, you cannot usevector.front()
afterward (just for example). Or justvector.push_back
will lead all caller iterators into invalid state (even worse of moving IMHO)! Of course, my comments here are quite general. – Bataan