0

In my experience Pattern 2 in following code nearly always fail and Pattern 1 always succeeds. What part of standard (if any) does guarantee that?

#include <iostream>
#include <cstdlib>

struct Data {
    int a;
    char b[5];
};

// Pattern 1
struct Base1 {
};

struct Derived1: Base1 {
    int a;
    char b[5];
};

static_assert(sizeof(Derived1)==sizeof(Data),  "Pattern 1 failed");

// Pattern 2
struct Base2 {
    int a;
    char b[5];
};

struct Derived2: Base2 {
};

static_assert(sizeof(Derived2)==sizeof(Data),  "Pattern 2 failed");

int main()
{
    std::cout << "Hello, world!" << std::endl;
}

Can Derived1and Data be considered structures with common beginning sequence?

curiousguy
  • 8,038
  • 2
  • 40
  • 58
Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • @S.M. fixed. That doesn't change fact that second one fails in some compilers, it's an abstract example. – Swift - Friday Pie Dec 06 '19 at 06:18
  • 1
    *"nearly always fail "* kindly provide at least *one* specific implementation where it fails, because either I'm astoundingly fortunate, or every toolchain I've tried, including gcc 6.1, 8.3, 9.2, clang 4.0.1, 6.0.0, 7.0.0, 8.0.0, icc 18.0 19.0.1, vc14.1, 14.2 all trigger no static asserts in this code. – WhozCraig Dec 06 '19 at 06:32
  • @WhozCraig well, you described those that WORK, which makes only one case out of my target platforms. all llvm-based ones , gcc 4.5 or later, Vc14 or later are those that don't assert. Anything prior , MSVC < v.14, lcc and PGI C++ which ARE my targets, and some others I'm not allowed to disclose, do fail, but I'm not sure if that should be written off as non-compliance or this is implementation-defined. Some fail only with certain padding\alignment configurations and it depends on content of `Data`. The question got `language-lawyer` tag – Swift - Friday Pie Dec 06 '19 at 06:40
  • @Scheff the link says that Empty base optimization required by standard layout types since C++11, which seems enough for me, because it's my use case. – Swift - Friday Pie Dec 06 '19 at 06:56
  • I just did read it completely and wasn't anymore sure whether it grants anything. It starts with _Allows the size of an empty base subobject to be zero._ which is not "shall" (in the sense of "must"). Then there is also the part about StandardLayoutTypes which I didn't get fully... However, I believe the link itself is worth: [Empty base optimization](https://en.cppreference.com/w/cpp/language/ebo). ;-) – Scheff's Cat Dec 06 '19 at 06:59
  • 1
    The C++ standard does not specify the size neither for Pattern1 neither for Pattern2. It actualy does not depend on the compiler but the ABI describing the target object file format. On elf platform (most Unixes, linux...) object layout depends also on the processor. Exemple of specs: [itanium-C++-abi][https://itanium-cxx-abi.github.io/cxx-abi/abi.html#POD] with [System-V-ps-abi-i386][https://www.uclibc.org/docs/psABI-i386.pdf]. For the plateforms/object-file-format you target, you need to find out the pertinent ABI specification. – Oliv Dec 06 '19 at 14:25
  • @Oliv +1 I'd accepted that as answer if it was one – Swift - Friday Pie Dec 06 '19 at 14:54

0 Answers0