So playing around with Move Semantics.
So my first look at this went like this:
class String
{
char* data;
int len;
public:
// Normal rule of three applied up here.
void swap(String& rhs) throw()
{
std::swap(data, rhs.data);
std::swap(len, rhs.len);
}
String& operator=(String rhs) // Standard Copy and swap.
{
rhs.swap(*this);
return *this;
}
// New Stuff here.
// Move constructor
String(String&& cpy) throw() // ignore old throw construct for now.
: data(NULL)
, len(0)
{
cpy.swap(*this);
}
String& operator=(String&& rhs) throw()
{
rhs.swap(*this);
return *this;
}
};
Looking at this. I though it may be worth defining the Move constructor in terms of Move assignment. It has a nice symmetry to it and I like it because it also looks DRY (and like copy and swap).
So I re-wrote the Move Constructor as:
String(String&& cpy) throw()
: data(NULL)
, len(0)
{
operator=(std::move(cpy));
}
But this generates an ambiguity error:
String.cpp:45:9: error: call to member function 'operator=' is ambiguous
operator=(std::move(rhs));
^~~~~~~~~
String.cpp:32:13: note: candidate function
String& operator=(String rhs)
^
String.cpp:49:13: note: candidate function
String& operator=(String&& rhs) throw()
^
1 error generated.
Since I used std::move()
while passing the argument I was expecting this to bind to the Move assignment operator. What am I doing wrong?