The test below exit with exitcode 1 due to bug in MSVC 7.1 implementation of the empty base optimization.
-----------------------------
#include <cstring>
#include <cstdio>
#include <new>
class empty_base { };
class base
{
public:
base (int val) : __val (val) { }
volatile int __val;
};
class derived1 : public base, public empty_base
{
public:
derived1 (int val) : base (val), empty_base () { }
};
class derived2 : public base, public empty_base
{
public:
derived2 (int val) : base (val) { }
};
template <class T>
int foo (const int val, const char* name)
{
const char fill = '\xdc';
char buf [sizeof (T) + 1];
std::memset (buf, fill, sizeof (buf));
T* t = new (buf) T (val);
int ret = 0;
if (fill != buf [sizeof (T)]) {
++ret;
std::printf ("buf [sizeof (%s)] expected %d, got %d\n",
name, int (fill), int (buf [sizeof (T)]));
}
if (val != t->__val) {
++ret;
std::printf ("%s::__val expected %d, got %d\n",
name, val, t->__val);
}
t->~T ();
return ret;
}
int main(int, char**)
{
const int init = 0xcdcdcdcd;
return foo <derived1> (init, "derived1")
+ foo <derived2> (init, "derived2");
}
-----------------------------
The test output:
-----------------------------
buf [sizeof (derived1)] expected -36, got 0
-----------------------------