cplusplus / cplusplus/draft

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

Open
#4,118 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

cwg
Dominant language
TeX
Stars
221
Forks
813
Avg merge
16h 4m
Merged PRs (30d)
36

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the basic.life#8 section in the draft and review its current examples and wording alongside the proposed C++ sample. Confirm with the standard's current lifetime rules whether the example is appropriate, then update the relevant sample and verify that the section accurately reflects the accepted behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.