1

In section 3.9.2 in document with draft of C++11:

(Document number N3242=11-0012, Date: 2011-02-28, Revises: N3225, Name: Working Draft, Standard for Programming Language C++) it have been said:

For any object (other than a base-class subobject) of "trivially copyable" type T, whether or not the object holds a valid value of type T, the underlying bytes (1.7) making up the object can be copied into an array of char or unsigned char.

I can not found any information about can I perform similar thing on objects with Standard Layout type.

Is it legal to make such operation on object with type, which is standard layout type?

Practically what I want todo that if std::is_standard_layout<T>::value is true then I (probably) can safely store object in auxiliary memory which is array of unsigned chars.


p.s. References

I know that due to policy of Stackoverlow user can be blocked but they are providing and links in the quesions, but here only for comfortable for reader. If links will be broken from 5 years please use another way to find mentioned documents.

[1] Drafts of C++ standards of The C++ Standards Committee - ISOCPP: http://www.open-std.org/jtc1/sc22/wg21/docs/standards

[2] Draft of C++2011 standard: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf

T.C.
  • 133,968
  • 17
  • 288
  • 421
Konstantin Burlachenko
  • 5,233
  • 2
  • 41
  • 40
  • 1
    There's "standard layout class", and there's "layout-compatible class". There's no such thing as "standard layout compatible". – T.C. Oct 18 '18 at 10:56
  • Thanks for pointing. I mean by compatible that "std::is_standard_layout::value" will be true. I will update question https://en.cppreference.com/w/cpp/types/is_standard_layout – Konstantin Burlachenko Oct 18 '18 at 11:00
  • @ShadowRanger, et al. You marked question as a duplicate. It is duplicate of what? – Konstantin Burlachenko Oct 18 '18 at 11:24
  • 1
    It's precise question about standard layout behaviour. It's not duplicate. More specifically in link that you provide @ShadowRanger the talk about standard layout is not arised explicitly with accent. – Konstantin Burlachenko Oct 18 '18 at 11:26
  • @bruziuz: The duplicate both question and answer, specifically address standard layout and its insufficiency. A duplicate need not be the exact same question if it addresses the question and answer demanded of the new question. [We closed another duplicate](https://stackoverflow.com/q/52767837/364696) of this question just last week, for the same reasons. – ShadowRanger Oct 18 '18 at 12:22

1 Answers1

3

No, you can't, unless you want to end up with objects with broken invariants.

It is easy to make a standard layout type which isn't trivially copyable type shows this:

struct A {
    A & operator=(A) {
        std::terminate();
    }
};

static_assert(    std::is_standard_layout   <A>::value, "");
static_assert(not std::is_trivially_copyable<A>::value, "");

If you copy the object using std::memcpy, the copy assignment operator won't be called, and the program continues. If you copy it normally, the program stops. This shows that std::memcpy does not respect the rules set up by the type.

A typical example where this is a problem: consider an object that holds a pointer that gets freed in the destructor. If you copy it with std::memcpy, you will copy the pointer, and when the destructors run, you will get a double free.

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • 1
    Thanks for answer. I create a backup of this quesion in https://sites.google.com/site/burlachenkok/download. Because probably quesion will be removed from stackoverlow as duplicate. – Konstantin Burlachenko Oct 18 '18 at 11:38
  • @bruziuz: That's not how duplicate marks work. It just means it's closed to new answers, and people who find this question are directed to the canonical source of answers. – ShadowRanger Oct 18 '18 at 22:05