Topic: placement new to "modify" references?


Author: squell@alumina.nl (Marc Schoolderman)
Date: Thu, 21 Jul 2005 21:04:10 GMT
Raw View
 > Intuitively, example 2 looks like it should be undefined but I can't
 > find any wording that says so.

This was a defect in the standard, you'll be interested to read:

   http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#89

~Marc.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Me" <anti_spam_email2003@yahoo.com>
Date: 20 Jul 2005 21:50:01 GMT
Raw View
// example 1
struct foo {
 const int a;
 foo(int a) : a(a) {}
 foo & operator=(const foo &f)
 {
  if (&f != this) {
   this->~foo();
   new (this) foo(f);
// undefined by 3.8/9
  }
  return *this;
 }
};

foo f(3);
f = foo(4);
cout << f.a;


// example 2
struct foo {
 int &a;
 foo(int &a) : a(a) {}
 foo & operator=(const foo &f)
 {
  if (&f != this) {
   this->~foo();
   new (this) foo(f);
// undefined?
  }
  return *this;
 }
};

int a = 3, b = 4;
foo f(a);
f = foo(b);
cout << f.a;

Intuitively, example 2 looks like it should be undefined but I can't
find any wording that says so.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]