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 9184211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:59:21+00:00 2026-06-17T18:59:21+00:00

This one has me thoroughly confused. In the below sample I get the error:

  • 0

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 void swap(const one& other) and remove the const to void swap(one& other) everything works. if I change the signature of 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)

Demo of it broken

Demo of it still broken, but this ‘fix’ works in VS2010

  • 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-06-17T18:59:23+00:00Added an answer on June 17, 2026 at 6:59 pm
    // other can't be const since swap modifies it
    void swap(one& other){ x.swap(other.x); }
    
    // Why swapping? (swap on rvalues don't work either since it's pointless.)
    //Just move the other
    void swap(one&& other){ x = std::move(other.x); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one has me pretty rattled so I thank you in advance for your
This one has been stumping me for a while. But I'm no expert. This
This one has me scratching my head, so I'm hoping a second pair of
This one has proven to be a little tricky for me so far. I
This one has me stumped. <div id=container> <div id=store></div> <div id=contact></div> </div> I'm looking
This one has me stumped. Here are the relative bits of code: public AgencyDetails(Guid
This one has made me a lot of stress over the last couple of
This one has me stumped. I need to call an activity method from within
I know this one has been asked few times already ( 1 , 2
It turns out that in order to do this one has to bind the

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.