standard-layout

Why is this struct not standard-layout?

此生再无相见时 提交于 2020-02-23 11:33:32
问题 A piece of code is worth a thousands words. #include <iostream> #include <type_traits> using namespace std; struct A { int a; }; struct B : A { int b; }; int main() { cout << is_standard_layout<B>::value << endl; // output false! WHY? return 0; } 回答1: From the definition of standard layout classes (§9 Classes, paragraph 7) [...] * 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

Why is this struct not standard-layout?

我的梦境 提交于 2020-02-23 11:33:24
问题 A piece of code is worth a thousands words. #include <iostream> #include <type_traits> using namespace std; struct A { int a; }; struct B : A { int b; }; int main() { cout << is_standard_layout<B>::value << endl; // output false! WHY? return 0; } 回答1: From the definition of standard layout classes (§9 Classes, paragraph 7) [...] * 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

Member of Union has User-Defined Constructor

旧巷老猫 提交于 2019-12-23 02:24:07
问题 For the following code: class Foo{ int foo; public: Foo() : foo(13) {} int getFoo() const { return foo; } }; union Bar{ Foo fBar; double dBar; }; I believe this is fully legal in C++. http://en.cppreference.com/w/cpp/language/union#Explanation says: If two union members are standard-layout types, it's well-defined to examine their common subsequence on any compiler And thus in gcc I can do this: Bar bar = { Foo() } When I try this in Visual Studio 2008 I get the error: error C2620: member Bar

Is a Union Member's Destructor Called

心已入冬 提交于 2019-12-17 16:32:20
问题 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,

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

孤街浪徒 提交于 2019-12-17 14:44:09
问题 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

Member of Union has User-Defined Constructor

落爺英雄遲暮 提交于 2019-12-07 17:11:30
For the following code: class Foo{ int foo; public: Foo() : foo(13) {} int getFoo() const { return foo; } }; union Bar{ Foo fBar; double dBar; }; I believe this is fully legal in C++. http://en.cppreference.com/w/cpp/language/union#Explanation says: If two union members are standard-layout types, it's well-defined to examine their common subsequence on any compiler And thus in gcc I can do this : Bar bar = { Foo() } When I try this in Visual Studio 2008 I get the error: error C2620: member Bar::fBar of union Bar has user-defined constructor or non-trivial default constructor Error C2620 states

C++ Standard Layout and References

我与影子孤独终老i 提交于 2019-12-07 02:19:52
问题 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? 回答1: 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

Common initial sequence and alignment

删除回忆录丶 提交于 2019-12-06 18:31:26
问题 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

Union of layout-compatible types

匆匆过客 提交于 2019-12-05 16:34:30
问题 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. 回答1: 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

How is is_standard_layout useful?

感情迁移 提交于 2019-12-05 14:21:36
问题 From what I understand, standard layout allows three things: Empty base class optimization Backwards compatibility with C with certain pointer casts Use of offsetof Now, included in the library is the is_standard_layout predicate metafunction, but I can't see much use for it in generic code as those C features I listed above seem extremely rare to need checking in generic code. The only thing I can think of is using it inside static_assert , but that is only to make code more robust and isn't