2

What is the reason that the second class is not standard layout? (Visual Studio C++)

#include <iostream>
#include <type_traits>

struct A
{
    int i;
};

struct B : public A
{
};

std::cout << "is_standard_layout<B> == "
          << std::boolalpha
          << std::is_standard_layout<B>::value // gives false
          << std::endl;
Xeo
  • 129,499
  • 52
  • 291
  • 397
mazatwork
  • 1,275
  • 1
  • 13
  • 20
  • It seems Visual Studio 10 just do not support classes like is_standard_layout yet. GCC 4.6 works. – mazatwork Aug 18 '11 at 14:43
  • what std::is_standard_layout<> is supposed to do ? i haven't found very much documentation about this feature. – Stephane Rolland Aug 18 '11 at 15:06
  • @Stephane: It's a metamethod that returns whether a class is standard layout. The [Wikipedia article](http://en.wikipedia.org/wiki/C%2B%2B0x#Modification_to_the_definition_of_plain_old_data) has a thorough discussion of what that means. – Nicol Bolas Aug 18 '11 at 18:19
  • @mazatwork, please accept the answer below or explain why it isn't adequate. – spraff Jan 18 '12 at 11:09

1 Answers1

2

Accorting to this MSVC supports built-in type traits since version 8 but this seems to say that you need version 11.

Section 9.7 defines a standard-layout class as 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 the same type as the first non-static data member.

There's some explanation here.

Community
  • 1
  • 1
spraff
  • 32,570
  • 22
  • 121
  • 229