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

  • SEARCH
  • Home
  • 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 6601883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:48:38+00:00 2026-05-25T18:48:38+00:00

EDIT: solved see comments –don’t know how to mark as solved with out an

  • 0

EDIT: solved see comments
–don’t know how to mark as solved with out an answer.

After watching a Channel 9 video on Perfect Forwarding / Move semantics in c++0x i was some what led into believing this was a good way to write the new assignment operators.

#include <string>
#include <vector>
#include <iostream>

struct my_type 
{
    my_type(std::string name_)
            :    name(name_)
            {}

    my_type(const my_type&)=default;

    my_type(my_type&& other)
    {
            this->swap(other);
    }

    my_type &operator=(my_type other)
    {
            swap(other);
            return *this;
    }

    void swap(my_type &other)
    {
            name.swap(other.name);
    }

private:
    std::string name;
    void operator=(const my_type&)=delete;  
    void operator=(my_type&&)=delete;
};


int main()
{
    my_type t("hello world");
    my_type t1("foo bar");
    t=t1;
    t=std::move(t1);
}

This should allow both r-values and const& s to assigned to it. By constructing a new object with the appropriate constructor and then swapping the contents with *this. This seems sound to me as no data is copied more than it need to be. And pointer arithmetic is cheap.

However my compiler disagrees. (g++ 4.6) And I get these error.

copyconsttest.cpp: In function ‘int main()’:
copyconsttest.cpp:40:4: error: ambiguous overload for ‘operator=’ in ‘t = t1’
copyconsttest.cpp:40:4: note: candidates are:
copyconsttest.cpp:18:11: note: my_type& my_type::operator=(my_type)
copyconsttest.cpp:30:11: note: my_type& my_type::operator=(const my_type&) <deleted>
copyconsttest.cpp:31:11: note: my_type& my_type::operator=(my_type&&) <near match>
copyconsttest.cpp:31:11: note:   no known conversion for argument 1 from ‘my_type’ to ‘my_type&&’
copyconsttest.cpp:41:16: error: ambiguous overload for ‘operator=’ in ‘t = std::move [with _Tp = my_type&, typename std::remove_reference< <template-parameter-1-1> >::type = my_type]((* & t1))’
copyconsttest.cpp:41:16: note: candidates are:
copyconsttest.cpp:18:11: note: my_type& my_type::operator=(my_type)
copyconsttest.cpp:30:11: note: my_type& my_type::operator=(const my_type&) <deleted>
copyconsttest.cpp:31:11: note: my_type& my_type::operator=(my_type&&) <deleted>

Am I doing something wrong? Is this bad practice (I don’t think there is way of testing whether you are self assigning)? Is the compiler just not ready yet?

Thanks

  • 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-25T18:48:39+00:00Added an answer on May 25, 2026 at 6:48 pm

    Be very leery of the copy/swap assignment idiom. It can be sub-optimal, especially when applied without careful analysis. Even if you need strong exception safety for the assignment operator, that functionality can be otherwise obtained.

    For your example I recommend:

    struct my_type 
    {
        my_type(std::string name_)
                :    name(std::move(name_))
                {}
    
        void swap(my_type &other)
        {
                name.swap(other.name);
        }
    
    private:
        std::string name;
    };
    

    This will get you implicit copy and move semantics which forward to std::string’s copy and move members. And the author of std::string knows best how to get those operations done.

    If your compiler does not yet support implicit move generation, but does support defaulted special members, you can do this instead:

    struct my_type 
    {
        my_type(std::string name_)
                :    name(std::move(name_))
                {}
    
        my_type(const mytype&) = default;
        my_type& operator=(const mytype&) = default;
        my_type(mytype&&) = default;
        my_type& operator=(mytype&&) = default;
    
        void swap(my_type &other)
        {
                name.swap(other.name);
        }
    
    private:
        std::string name;
    };
    

    You may also choose to do the above if you simply want to be explicit about your special members.

    If you’re dealing with a compiler that does not yet support defaulted special members (or implicit move members), then you can explicitly supply what the compiler should eventually default when it becomes fully C++11 conforming:

    struct my_type 
    {
        my_type(std::string name_)
                :    name(std::move(name_))
                {}
    
        my_type(const mytype& other)
            : name(other.name) {}
        my_type& operator=(const mytype& other)
        {
            name = other.name;
            return *this;
        }
        my_type(mytype&& other)
            : name(std::move(other.name)) {}
        my_type& operator=(mytype&& other)
        {
            name = std::move(other.name);
            return *this;
        }
    
        void swap(my_type &other)
        {
                name.swap(other.name);
        }
    
    private:
        std::string name;
    };
    

    If you really need strong exception safety for assignment, design it once and be explicit about it (edit to include suggestion by Luc Danton):

    template <class C>
    typename std::enable_if
    <
        std::is_nothrow_move_assignable<C>::value,
        C&
    >::type
    strong_assign(C& c, C other)
    {
        c = std::move(other);
        return c;
    }
    
    template <class C>
    typename std::enable_if
    <
        !std::is_nothrow_move_assignable<C>::value,
        C&
    >::type
    strong_assign(C& c, C other)
    {
        using std::swap;
        static_assert(std::is_nothrow_swappable_v<C>,  // C++17 only
                      "Not safe if you move other into this function");
        swap(c, other);
        return c;
    }
    

    Now your clients can choose between efficiency (my type::operator=), or strong exception safety using strong_assign.

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

Sidebar

Related Questions

Edit: I have solved this by myself. See my answer below I have set
EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
EDIT 3: Solved. See below. EDIT 2: I think Chadwick's on the right track
SOLVED see Edit 2 Hello, I've been writing a Perl program to handle automatic
((Answer selected - see Edit 5 below.)) I need to write a simple pink-noise
Edit : Solved, there was a trigger with a loop on the table (read
Edit: Solved. Hi, I'm starting with Qt, I try to connect a slot to
Edit: From another question I provided an answer that has links to a lot

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.