Topic: unclear clause 3.8/7


Author: brok@rubikon.pl (Bronek Kozicki)
Date: Fri, 12 Nov 2004 15:52:44 GMT
Raw View
What's behaviour of following program? I'd expect it to be:
1. valid under ISO 14882:1998
2. implicit undefined behaviour under ISO 14882:2003


I'm referring to clause 3.8/7, third bullet (introduced in TC1), that is:

---- citation begin
If, after the lifetime of an object has ended and before the storage=20
which the object occupied is reused or released, a new object is created=20
at the storage location which the original object occupied, a pointer=20
that pointed to the original object, a reference that referred to the=20
original object, or the name of the original object will automatically=20
refer to the new object and, once the lifetime of the new object has=20
started, can be used to manipulate the new object, if: [...]
=97 the type of the original object is not const-qualified, and, if a=20
class type, does not contain any non-static data member whose type is=20
const-qualified or a reference type, [...]
---- citation end


Or maybe it's explicit undefined behaviour per following paragraphs or=20
another clause?


B.


PS. my question is referring to Item 6 in "Exceptional C++ Style", Herb=20
Sutter.



#include <iostream>     // std::cout, used in main


template <typename T>
void destroy(T* p)
{
   p->~T();
}

template <typename T>
void construct(T* p, const T& src)
{
   new (static_cast<void *>(p)) T(src);
}

template <typename T>
void swap(T& a, T& b)
{
   if (&a !=3D &b)
   {
     T t(a);

     destroy(&a);
     construct(&a, b);

     destroy(&b);
     construct(&b, t);
   }
}

struct A
{
   const int i;
};

int main()
{
   A a1 =3D {1};
   A a2 =3D {2};

   std::cout << "a1.i =3D " << a1.i << " a2.i =3D " << a2.i << std::endl;
   swap(a1, a2);

   // are a1 and a2 still valid? In what state these objects are?
   std::cout << "a1.i =3D " << a1.i << " a2.i =3D " << a2.i << std::endl;
}



---
[ 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: ron@sensor.com (Ron Natalie)
Date: Sat, 13 Nov 2004 02:50:24 GMT
Raw View
Bronek Kozicki wrote:
> What's behaviour of following program? I'd expect it to be:
> 1. valid under ISO 14882:1998
> 2. implicit undefined behaviour under ISO 14882:2003
>

It's clearly undefined behavior under the revision as it SHOULD BE.
In main, a1.i and a2.i are const and should not be legitimately
changed.   The 7.1.5.1 prohibition that normally places modifiation
of const things by heroic methods (const_cast etc...) ends at the
end of the object lifetime, so you're right it's ambiguous without
the 2003 addition.

---
[ 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                       ]