Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 691885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:35:31+00:00 2026-05-14T02:35:31+00:00

I have this code which compiles and works as expected: class Right {}; class

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:35:32+00:00Added an answer on May 14, 2026 at 2:35 am

    Use:

     std::copy(rightLst.begin(), rightLst.end(), std::back_inserter(leftLst));
    

    This will add new elements to the end of leftLst corresponding to the elements in rightLst. You will need to add a constructor to Left that takes const Right &.

    If leftLst and rightLst already contain the same number of elements and you want to overwrite the elements in leftLst, use:

    std::copy(rightLst.begin(), rightLst.end(), leftLst.begin());
    

    Edit:

    Even simpler is to use:

    leftLst.assign(rightLst.begin(), rightLst.end());
    

    This will replace all elements (if any) in leftLst, so it will cover both cases given above.

    The reason that the normal assignment operator doesn’t work here is that the standard defines it to only accept a list of the same type, so it won’t allow assigning from list<Right> to list<Left>.


    If the constructor is declared explicit, there won’t be a way for the assign function to know how to convert Right to Left, so it won’t work. Another option is to define a conversion operator on Right instead:

    Right::operator Left() { return Left();}
    

    If you do neither, assign will fail to compile.

    The second version of copy given above will work without a conversion operator or a conversion constructor (only an assignment operator is needed), but it requires the target list to have enough elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code which compiles and works as expected: #include <iostream>
I have this code (which is way simplified from the real code): public interface
I have a source directory which uses makefile to compile the code. This makefile/configure
I have this code which adds a x number of views to a Multiview
I have this code which does the trick: #include <stdio.h> int main() { int
I have this code which is called at an onChange event of an function
I have this code which gets WP posts, but it gets all the posts
I have this code which will include template.php file from inside each of these
I have this code which should create a splash image with either no animation
I have this code which is passed to Paypal checkout: <?php foreach ($paypalBasket as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.