13

Why is that undefined behaviour?

struct s
{
    const int id; // <-- const member

    s(int id):
        id(id)
    {}

    s& operator =(const s& m) {
        return *new(this) s(m); // <-- undefined behavior?
    }
};

(Quote from the standard would be nice).

This question arose from this answer.

16
  • 1
    const int id; says that the value of id will never change. And then you change it?
    – Bo Persson
    Nov 24, 2017 at 12:57
  • 4
    @BoPersson: Another view is that I create a new object on the same location.
    – DaBler
    Nov 24, 2017 at 14:08
  • 1
    I distinctively remember this being legal. @BoPersson const only applies to the lifetime of the object.
    – Passer By
    Nov 24, 2017 at 14:43
  • 1
    @YSC Destructors not being called isn't undefined behaviour. Calling a destructor on an invalid object is.
    – Passer By
    Nov 24, 2017 at 14:50
  • 1
    @YSC If the destructor is trivial (like in this case), then it's legal to not call it. timsong-cpp.github.io/cppwp/basic.life#5.sentence-1
    – Rakete1111
    Nov 24, 2017 at 14:54

1 Answer 1

20

There is nothing that makes the shown code snippet inherently UB. However, it is almost certain UB will follow immediately under any normal usage.

From [basic.life]/8 (emphasis mine)

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 storage for the new object exactly overlays the storage location which the original object occupied, and

  • the new object is of the same type as the original object (ignoring the top-level cv-qualifiers), and

  • 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, and

  • the original object was a most derived object of type T and the new object is a most derived object of type T (that is, they are not base class subobjects).

Since there is a const member in s, using the original variable after a call to operator= will be UB.

s var{42};
var = s{420};         // OK
do_something(var.id); // UB! Reuses s through original name
do_something(std::launder(&var)->id);  // OK, this is what launder is used for
5
  • So, do I understand correctly that there is no legal way to access the members prior C++17?
    – DaBler
    Nov 24, 2017 at 15:44
  • 4
    @DaBler Technically, there is. But I highly recommend against it. auto& ref = (var = s{420});. And then use ref
    – Passer By
    Nov 24, 2017 at 15:47
  • OK, I get it. Thanks.
    – DaBler
    Nov 24, 2017 at 16:04
  • @DaBler Technically, under a strict reading on the std, on common implementations, using a pointer object was always guaranteed to work after that const changing trick. Still is, as long as a pointer contains only an address (number). However, this is pedantic and certainly NOT intended, and the people whose work is to interpret the std would not support it. No compiler will go out of its way to support that pedantic reading.
    – curiousguy
    Jul 6, 2018 at 21:37
  • 3
    This answer may not be correct anymore, since the quoted sentence was relaxed in C++20: github.com/cplusplus/draft/commit/… Dec 31, 2021 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.