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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:26:22+00:00 2026-05-22T18:26:22+00:00

I was implementing (for training purpose) a Bubble Sort template function: template<typename iterInput, typename

  • 0

I was implementing (for training purpose) a Bubble Sort template function:

template<typename iterInput,
         typename predicate>
void BubbleSort(iterInput first1,iterInput last1,predicate func)
{
    bool swapped(false);
    do
    {
        swapped = false;
        iterInput begin = first1;
        iterInput beginMinus = first1;
        ++begin;
        for (;begin != last1; begin++,beginMinus++)
        {
            if (func(*beginMinus,*begin) )
            {
                std::swap(*beginMinus,*begin);
                swapped = true;
            }
        }
    }
    while(swapped);
}

When I have realized that this function will not work for class with no assignment operator, like this one (forgive me for the bad name):

class NoCopyable
{
public:
    explicit NoCopyable(int value) : value_(value) {}
    NoCopyable(const NoCopyable& other) : value_(other.value_) {}
    ~NoCopyable() {}
    bool operator<(const NoCopyable& other) { return value_ < other.value_; }
    void setValue(int value) { value_ = value; }
    std::ostream& print(std::ostream& os) const { return os << value_; }
private:
    NoCopyable& operator=(const NoCopyable& other);
    int value_;
};

std::ostream& operator<<(std::ostream& os, const NoCopyable& obj)
{
    return obj.print(os);
}

struct PrintNoCopyable
{
    void operator()(const NoCopyable& noCopyable) { std::cout << noCopyable << '\n'; }
};

The compiler raises this error Error 1 error C2248: ‘NoCopyable::operator =’ : cannot access private member declared in class ‘NoCopyable’

So, I have slightly modify the code using instead of the std::swap function my version of the swap function, here is the code:

template<typename T1,
         typename T2>
void noAssignmentSwap(T1& t1,T2& t2)
{
    T1 temp(t1);
    t1.~T1();
    new (&t1) T1(t2);
    t2.~T2();
    new (&t2) T2(temp);
}

The code compiles and gives the right result. However I am not completely sure, I remember a Sutter’s article that suggest you to avoid playing with the objects life time. The article just warns you by playing with fire without actually giving you any real reason. I can see problem in exception safety if the copy constructor of T1 or T2 can throw. However there is the same problem in the standard version if the assignment operator is allowed to throw.

Here the question, can you see any possible drawbacks in this version of swap?

Cheers

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

    The difference is that when the assignment operator fails, you still have the same number of objects.

    If you destroy one object and fail to create a new one, one object is lost! If it was part of a container, the container’s state is probably also invalid.

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

Sidebar

Related Questions

Implementing a datastructure with two function pointers, key_eq and key_hash . When these function
Is implementing a code generator a sort of model driven design/ model driven software
I'm reading the book Programming Challenges: The Programming Contest Training Manual and are implementing
Implementing Equals() for reference types is harder than it seems. My current canonical implementation
Implementing a 'sandbox' environment in Python used to be done with the rexec module
Implementing the ScriptControlClass was extremely easy, unfortunately the side effects with the language implementation
implementing publishActivity in PHP using the REST API using this code: $activity = array(
When implementing a needle search of a haystack in an object-oriented way, you essentially
When implementing Quicksort, one of the things you have to do is to choose
I implementing a EventQueue and get notified when AWTEvents are send. I wait till

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.