3

Have been reading a few strict aliasing questions, such as Cast array of bytes to POD or Aliasing `T*` with `char*` is allowed. Is it also allowed the other way around?

From these I gather that the only legal way to access a memory location declared to be any type (specifically also (array of) char) as another type is to invoke placement new on it, as that would change the dynamic type.

Since std::aligned_storage normally has to have an underlying type other than the intended use type, it seems to me it is impossible to use the storage without invoking placement new on it first.

So I would not be allowed to create aligned_storage for, e.g. a double and use it as a double via pointer casting? Or rather, before I would be allowed to access the memory as double via pointer cast, I'd have to do a placement new on it, "turning it into" a dynamic object of type double?

Community
  • 1
  • 1
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • 2
    http://stackoverflow.com/questions/13466556/aligned-storage-and-strict-aliasing – Martin Ba Jan 10 '17 at 22:44
  • 2
    Note that the Standard says the lifetime of an object with *vacuous initialization* begins "when storage with the proper alignment and size is obtained". `[basic.life]` – Ben Voigt Jan 10 '17 at 22:51
  • 1
    I think `std::aligned_storage` isn't special, making this is a dupe of https://stackoverflow.com/questions/37644977/is-circumventing-a-class-constructor-legal-or-does-it-result-in-undefined-behav. Can you check if you think there's something else going on here? –  Jan 10 '17 at 22:59
  • 1
    @BenVoigt According to the answer in that other question I linked to though, without placement new, there's no object, so there's no lifetime to consider. (You took part in the comments there, even.) –  Jan 10 '17 at 23:00
  • 1
    @hvd: Well by definition an object is a region of storage, and a region of storage certainly exists. And in C, you can set the dynamic type of a region of storage by writing to it, the type is copied from the source expression to the destination region. Breaking compatibility with that rule would be quite bad. – Ben Voigt Jan 10 '17 at 23:09
  • @hvd: Yes I took part in the comments there. And agreed with an answer other than the accepted one (your use of the definite article is invalid, there is no "the answer" on that page, there are many). – Ben Voigt Jan 10 '17 at 23:10
  • @BenVoigt That's not how "object" is defined any more, and whether it's a good idea or not according to you or to me does not change anything. As for "the answer", I meant "the accepted answer", sorry for leaving that out. –  Jan 10 '17 at 23:14
  • @hvd: Did I miss a [tag:c++1z] on this question? It's good to note that there's a plan to change the definition of *object*, but it hasn't happened yet. – Ben Voigt Jan 10 '17 at 23:17
  • @BenVoigt: There's a need for a term to describe a region of storage which may be accessed in the same fashion as a like-sized array of character type. If the term "object" is defined as something else, what would be a suitable retronym to replace that sense of the term "object"? – supercat Jan 11 '17 at 20:39

0 Answers0