I cannot figure out if the std::move in the following code does anything good or that it is completely wrong?
The class Object
has both Move and Copy constructor defined.
First: With Move:
template<typename T> template <typename F>
const Object<T> Object<T>::operator*(const F& rhs) const
{
return std::move(Object(*this) *= rhs); // We end in move constructor
}
Second: Without Move:
template<typename T> template <typename F>
const Object<T> Object<T>::operator*(const F& rhs) const
{
return Object(*this) *= rhs; // We end in copy constructor
}
The *=
operator is defined as:
template<typename T> template<typename F>
Object<T>& Object<T>::operator*=(const F& rhs)
{
for(int i = 0; i < dimension ; i++)
{
_inner[i] *= rhs;
}
return *this;
}
Here is the code i use to test it:
Object<double> test(4);
Object<double> test2(test * 4);
std::cout << test2; // works fine
Result
In the first case we end in the move constructor and in the second we end in the copy constructor.
In either case the code compiles.
Is one more efficient than the other since I would assume it is faster to move the new object out instead of copying it out?
Additional info:
I use the following compiler: g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…