I have this code which compiles and works as expected:
class Right
{};
class Left
{
public:
Left& operator = (Right const&)
{
//... Do something ...
return *this;
}
};
int main()
{
Right right;
Left left;
// Assign individual object -- this works
left = right;
}
But now, this one surprises me, I thought the template would work itself out since I already provided the = operator() to the Left class.
int main()
{
...
std::list<Right> rightLst;
std::list<Left> leftLst;
// Assign a list of objects -- this doesn't compile
leftLst = rightLst;
}
What can I do so that I could convert the rightLst to leftLst conversion in a single line?
Also, what if explicit keyword is used on Left? Then none of the proposed solutions will work, i.e. if Left is defined as:
class Left
{
public:
Left()
{}
explicit Left(Right const& right_)
{
.. // Do something ..
}
Left& operator = (Right const& right_)
{
// .. Do something ..
return *this;
}
};
See, I'm trying to avoid writing this code below to achieve what I want to do:
for(std::list<Right>::iterator it = rightLst.begin();
it != rightLst.end();
it++ )
{
leftLst.push_back(Left(*it));
}
What code from STL I could re-use to replace the above?