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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:45:15+00:00 2026-05-18T01:45:15+00:00

I answered the question about std::vector of objects and const-correctness , and received a

  • 0

I answered the question about std::vector of objects and const-correctness, and received a comment about undefined behavior. I do not agree and therefore I have a question.

Consider the class with const member:

class A { 
public: 
    const int c; // must not be modified! 
    A(int c) : c(c) {} 
    A(const A& copy) : c(copy.c) { }     
    // No assignment operator
}; 

I want to have an assignment operator but I do not want to use const_cast like in the following code from one of the answers:

A& operator=(const A& assign) 
{ 
    *const_cast<int*> (&c)= assign.c;  // very very bad, IMHO, it is undefined behavior
    return *this; 
} 

My solution is

// Custom-defined assignment operator
A& operator=(const A& right)  
{  
    if (this == &right) return *this;  

    // manually call the destructor of the old left-side object
    // (`this`) in the assignment operation to clean it up
    this->~A(); 
    // use "placement new" syntax to copy-construct a new `A` 
    // object from `right` into left (at address `this`)
    new (this) A(right); 
    return *this;  
}  

Do I have undefined behavior (UB)?

What would be a solution without UB?

  • 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-18T01:45:16+00:00Added an answer on May 18, 2026 at 1:45 am

    Your code causes undefined behavior.

    Not just “undefined if A is used as a base class and this, that or the other”. Actually undefined, always. return *this is already UB, because this is not guaranteed to refer to the new object.

    Specifically, consider 3.8/7:

    If, after the lifetime of an object
    has ended and before the storage which
    the object occupied is reused or
    released, a new object is created at
    the storage location which the
    original object occupied, a pointer
    that pointed to the original object, a
    reference that referred to the
    original object, or the name of the
    original object will automatically
    refer to the new object and, once the
    lifetime of the new object has
    started, can be used to manipulate the
    new object, if:

    …

    — the type of the original object is
    not const-qualified, and, if a class
    type, does not contain any non-static
    data member whose type is
    const-qualified or a reference type,

    Now, “after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied” is exactly what you are doing.

    Your object is of class type, and it does contain a non-static data member whose type is const-qualified. Therefore, after your assignment operator has run, pointers, references and names referring to the old object are not guaranteed to refer to the new object and to be usable to manipulate it.

    As a concrete example of what might go wrong, consider:

    A x(1);
    B y(2);
    std::cout << x.c << "\n";
    x = y;
    std::cout << x.c << "\n";
    

    Expect this output?

    1
    2
    

    Wrong! It’s plausible you might get that output, but the reason const members are an exception to the rule stated in 3.8/7, is so that the compiler can treat x.c as the const object that it claims to be. In other words, the compiler is allowed to treat this code as if it was:

    A x(1);
    B y(2);
    int tmp = x.c
    std::cout << tmp << "\n";
    x = y;
    std::cout << tmp << "\n";
    

    Because (informally) const objects do not change their values. The potential value of this guarantee when optimizing code involving const objects should be obvious. For there to be any way to modify x.c without invoking UB, this guarantee would have to be removed. So, as long as the standard writers have done their job without errors, there is no way to do what you want.

    [*] In fact I have my doubts about using this as the argument to placement new – possibly you should have copied it to a void* first, and used that. But I’m not bothered whether that specifically is UB, since it wouldn’t save the function as a whole.

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

Sidebar

Related Questions

In the past I answered a question about who was wrong in regards to
I asked a question about bigints yesterday which was kindly answered. However, I have
On some StackOverflow question about GWT cons and pros from 2009 someone answered that:
An earlier question was answered about getting the OrderID and the number of associated
Perhaps a simple to be answered question, but I did not find a satisfying
I had a question answered which raised another one, why following does not work?
I have an answered StackOverflow question about how to combine to legacy CHAR database
Recently, I asked (and answered) a question on StackOverflow about why a unit test
i answered one question about closures here in SO with this sample: function Constructor()
My previous question about this subject was answered and I got some tests working

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.