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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:38:27+00:00 2026-05-28T13:38:27+00:00

The code is following: #include <iostream> #include <vector> #include <algorithm> using namespace std; struct

  • 0

The code is following:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct A {
  A(int i = -1): i_(i) {
    wcout << "Constructor: i = " << i_ << endl;
  }
  A(A const &a) {
    wcout << "Copy constructor: i = " << i_ << " a.i = " << a.i_ << endl;
    *this = a;
  }
  ~A() { wcout << "Destructor: i = " << i_ << endl; }
  A &operator=(A const& a) {
    wcout << "Copy assignment operator: i = " << i_ << " a.i = " << a.i_ << endl;
    i_ = a.i_;
    return *this;
  }
  bool operator==(A const& rhs) { return i_ == rhs.i_; }
  int get() { return i_; }
private:
  int i_;
};

int wmain() {
  A a[] = {1, 2, 3, 2, 4, 5};
  vector<A> v(a, a + sizeof a/sizeof a[0]);
  wcout << "==== Just before remove ====" << endl;
 remove(v.begin(), v.end(), 2);
  wcout << "==== Just after remove ====" << endl;

  return 0;
}

Output:

==== Just before remove ====
Constructor: i = 2
Destructor: i = 2
Constructor: i = 2
Destructor: i = 2
Constructor: i = 2
Destructor: i = 2
Copy assignment operator: i = 2 a.i = 3
Constructor: i = 2
Destructor: i = 2
Constructor: i = 2
Destructor: i = 2
Copy assignment operator: i = 3 a.i = 4
Constructor: i = 2
Destructor: i = 2
Copy assignment operator: i = 2 a.i = 5
==== Just after remove ====

The question is: why destructor is called 6 times while remove() was running? I need this mess to be clarified.

NOTE: execute this code on your system, please, before answering.
NOTE: MSVCPP 11

  • 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-28T13:38:28+00:00Added an answer on May 28, 2026 at 1:38 pm

    The question is: why destructor is called 6 times while remove() was
    running?

    In summary, the destructor calls have to do with the 2 getting implicitly converted to A by remove(). Every time the result of such an implicit conversion goes out of scope, A‘s destructor gets called.

    The reason for those implicit conversions is that remove() needs to compare every element of a to 2. The only way to do this is by calling A::operator==(const A&):

      bool operator==(A const& rhs) { ... }
    

    Since rhs is of type const A&, the compiler:

    1. calls A(int) to convert the 2 to an A;
    2. calls operator==(const A&);
    3. calls A::~A() to destroy the temporary.

    The latter are the destructor calls that you’re seeing.

    If you were to add the following comparison operator to A, you’ll see those destructor calls disappear:

      bool operator==(int rhs) { return i_ == rhs; }
    

    Alternatively, if you were to call remove() like so, you’ll see all bar one destructor calls disappear:

    remove( v.begin(), v.end(), A(2) );
    

    Finally, if you were to make A::A(int) explicit, the compiler would not allow you to call remove() with 2 as the last argument (you’d have to call it with A(2)).

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

Sidebar

Related Questions

Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
The following code #include <iostream> using namespace std; int main() { const char* const
For the following code: #include<iostream> #include<vector> #include<string> using namespace std; struct Test { string
I have the following code #include <iostream> #include <vector> using namespace std; int distance(vector<int>&
Consider the following code: #include <iostream> using namespace std; int main() { int x,
I have the following code compiled by gcc: #include <iostream> using namespace std; class
I have the following code #include <algorithm> #include <iostream> #include <vector> #include <functional> int
Consider the following code. #include <stdio.h> #include <vector> #include <iostream> struct XYZ { int
I have the following code: #include <iostream> #include <vector> #include <algorithm> int main( int
C++ returns invalid value in the following code: #include <iostream> #include <vector> using namespace

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.