standard-layout

C++ Standard Layout and References

喜你入骨 提交于 2019-12-05 05:14:56
According to the C++ standard: A standard-layout class is a class that: —has no non-static data members of type non-standard-layout class (or array of such types) or reference. What property(ies) of references prevent classes with reference members from being included in the definition of a standard layout class? A standard layout class is all about having a well defined layout for a particular type in memory . In C++, references aren't objects so don't have any storage that can be accessed in a well defined way by a conforming program even though the implementation will usually have to have

Common initial sequence and alignment

懵懂的女人 提交于 2019-12-05 00:03:22
While thinking of a counter-example for this question , I came up with: struct A { alignas(2) char byte; }; But if that's legal and standard-layout, is it layout-compatible to this struct B ? struct B { char byte; }; Furthermore, if we have struct A { alignas(2) char x; alignas(4) char y; }; // possible alignment, - is padding // 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 // x - - - y - - - x - - - y - - - struct B { char x; char y; }; // no padding required union U { A a; B b; } u; Is there a common initial sequence for A and B ? If so, does it include A::y & B::y ? I.e., may we write

Union of layout-compatible types

佐手、 提交于 2019-12-04 01:27:44
Look at this code: struct A { short s; int i; }; struct B { short s; int i; }; union U { A a; B b; }; int fn() { U u; u.a.i = 1; return u.b.i; } Is it guaranteed that fn() returns 1 ? Note: this is a follow-up question to this . NathanOliver- Reinstate Monica Yes, this is defined behavior. First lets see what the standard has to say about A and B . [class.prop]/3 has A class S is a standard-layout class if it: has no non-static data members of type non-standard-layout class (or array of such types) or reference, has no virtual functions and no virtual base classes, has the same access control

Standard-layout and tail padding

霸气de小男生 提交于 2019-11-30 08:10:14
David Hollman recently tweeted the following example (which I've slightly reduced): struct FooBeforeBase { double d; bool b[4]; }; struct FooBefore : FooBeforeBase { float value; }; static_assert(sizeof(FooBefore) > 16); //---------------------------------------------------- struct FooAfterBase { protected: double d; public: bool b[4]; }; struct FooAfter : FooAfterBase { float value; }; static_assert(sizeof(FooAfter) == 16); You can examine the layout in clang on godbolt and see that the reason the size changed is that in FooBefore , the member value is placed at offset 16 (maintaining a full

Standard-layout and tail padding

会有一股神秘感。 提交于 2019-11-29 11:12:39
问题 David Hollman recently tweeted the following example (which I've slightly reduced): struct FooBeforeBase { double d; bool b[4]; }; struct FooBefore : FooBeforeBase { float value; }; static_assert(sizeof(FooBefore) > 16); //---------------------------------------------------- struct FooAfterBase { protected: double d; public: bool b[4]; }; struct FooAfter : FooAfterBase { float value; }; static_assert(sizeof(FooAfter) == 16); You can examine the layout in clang on godbolt and see that the

Is a Union Member's Destructor Called

╄→гoц情女王★ 提交于 2019-11-27 23:31:04
C++11 allowed the use of standard layout types in a union : Member of Union has User-Defined Constructor My question then is: Am I guaranteed the custom destructor will be called, when the union goes out of scope? My understanding is that we must manually destroy and construct when switching: http://en.cppreference.com/w/cpp/language/union#Explanation But what about an example like this: { union S { string str; vector<int> vec; ~S() {} } s = { "Hello, world"s }; } When s goes out of scope, have I leaked the memory the string allocated on the heap because I did not call string 's destructor?

Can I legally reinterpret_cast between layout-compatible standard-layout types?

穿精又带淫゛_ 提交于 2019-11-27 16:06:14
I'm writing a class that, assuming the answer to Are enumeration types layout compatible with their underlying type? is "yes", is layout-compatible struct kevent but uses enum class es for filter , flags , etc. with the proper underlying types for the relevant fields. It is also standard-layout (the fields are all private and all themselves standard layout, there are no virtual members, there are no base classes). From my reading of n3690 , I can determine that my class and struct kevent have the same value representation, but I can't see anything in the standard that therefore allows me to

Why is C++11&#39;s POD “standard layout” definition the way it is?

為{幸葍}努か 提交于 2019-11-26 12:24:01
I'm looking into the new, relaxed POD definition in C++11 (section 9.7) A standard-layout class is a class that: has no non-static data members of type non-standard-layout class (or array of such types) or reference, has no virtual functions (10.3) and no virtual base classes (10.1), has the same access control (Clause 11) for all non-static data members, has no non-standard-layout base classes, either has no non-static data members in the most derived class and at most one base class with non-static data members , or has no base classes with non-static data members, and has no base classes of

What are Aggregates and PODs and how/why are they special?

这一生的挚爱 提交于 2019-11-25 21:38:21
问题 This FAQ is about Aggregates and PODs and covers the following material: What are Aggregates ? What are POD s (Plain Old Data)? How are they related? How and why are they special? What changes for C++11? 回答1: How to read: This article is rather long. If you want to know about both aggregates and PODs (Plain Old Data) take time and read it. If you are interested just in aggregates, read only the first part. If you are interested only in PODs then you must first read the definition,