This one has me thoroughly confused. In the below sample I get the error:
error C2664: ‘void std::unique_ptr<_Ty>::swap(std::unique_ptr<_Ty>
&&)’ : cannot convert parameter 1 from ‘const std::unique_ptr<_Ty>’ to
‘std::unique_ptr<_Ty> &&’
I have no idea whatsoever how it’s ending up at my swap function, or why it’s a problem. Interestingly if I change the signature of if I change the signature of void swap(const one& other) and remove the const to void swap(one& other) everything works.void swap(const one& other) and remove the const to void swap(one& other) it compiles in VS2010 but is still broken in GCC. If there are no swap overloads there is no problem.
//-----------------------------------------------------------------------------
class one
{
public:
one(){}
one(one&& other) : x(std::move(other.x)) {}
one& operator=(one&& other){ x = std::move(other.x); return *this; }
void swap(const one& other){ x.swap(other.x); }
void swap(one&& other){ x.swap(std::move(other.x)); }
private:
one(const one&);
one& operator=(const one&);
std::unique_ptr<int> x;
};
//-----------------------------------------------------------------------------
void swap(one& left, one& right)
{
left.swap(right);
}
//-----------------------------------------------------------------------------
void swap(one&& left, one& right)
{
right.swap(std::move(left));
}
//-----------------------------------------------------------------------------
void swap(one& left, one&& right)
{
left.swap(std::move(right));
}
//-----------------------------------------------------------------------------
class two
{
public:
two(){}
two(two&&){}
two& operator=(two&&){ return *this; }
operator one(){return one();}
private:
two(const two&);
two& operator=(const two&);
};
//-----------------------------------------------------------------------------
int main()
{
std::vector<two> twos(10);
std::vector<one> ones(std::make_move_iterator(twos.begin()), std::make_move_iterator(twos.end()));
}
Edit:
The non-constness requirement makes sense. Entirely an oversight on my part. Why is it calling swap in the first place though?
(For reference, I’m using VS2010)
1 Answer