Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve / extend sample in [basic.life#8] #4118

Open
bernd5 opened this issue Aug 19, 2020 · 0 comments
Open

Improve / extend sample in [basic.life#8] #4118

bernd5 opened this issue Aug 19, 2020 · 0 comments
Labels
cwg Issue must be reviewed by CWG.

Comments

@bernd5
Copy link

bernd5 commented Aug 19, 2020

If I understand the current version of the standard correctly the following code would not be UB (please correct me if I'm wrong):

#include <iostream>

struct C {
  const int i;
  void f() const {
     std::cout << i << "\n";
  }
  C(int i) : i(i) {}
  C& operator=( const C& );
};

C& C::operator=( const C& other) {
  if ( this != &other ) {
    this->~C();                 // lifetime of *this ends
    new (this) C(other);        // new object of type C created
    f();                        // well-defined
  }
  return *this;
}

int main(){
    C c1(1);
    C c2(2);
    c1 = c2;                        // well-defined
    c1.f();
}

The important point is that with the new rules we allow the definition of a user defined assignment operator for non complete const objects with const sub-objects.
Could we mention this in a sample?

@bernd5 bernd5 changed the title Improve sample in [basic.life#8] Improve / extend sample in [basic.life#8] Aug 19, 2020
@jensmaurer jensmaurer added the decision-required A decision of the editorial group (or the Project Editor) is required. label Sep 8, 2020
@jensmaurer jensmaurer added cwg Issue must be reviewed by CWG. and removed decision-required A decision of the editorial group (or the Project Editor) is required. labels Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cwg Issue must be reviewed by CWG.
Projects
None yet
Development

No branches or pull requests

2 participants