What is the size limit for a class?

In C++11 this is Annex B. Implementations can impose limits, but they should be at least:

  • Size of an object [262 144].
  • Data members in a single class [16 384].
  • Members declared in a single class [4 096].

The third one isn't directly relevant to the kind of construction you're using, I mention it just because it indicates that the second one is indeed the total members, presumably including those in bases and I'm not sure about members-of-members. But it's not just about the members listed in a single class definition.

Your implementation appears to have given up either 2^31 data members, or at size 2^32, since it accepts I but not J. It's fairly obviously reasonable for a compiler to refuse to consider classes with size greater than SIZE_MAX, even if the program happens not to instantiate it or use sizeof on the type. So even with the best possible effort on the part of the compiler I wouldn't ever expect this to work on a 32 bit implementation.

Note that "these quantities are only guidelines and do not determine compliance", so a conforming implication can impose an arbitrary smaller limit even where it has sufficient resources to compile a program that uses larger numbers. There's no minimum limit for conformance.

There are various opportunities in the C++ standard for a conforming implementation to be useless due to ridiculously small resource limits, so there's no additional harm done if this is another one.

C++03 is more-or-less the same:

  • Size of an object [262 144].
  • Data members in a single class, structure, or union [16 384].
  • Members declared in a single class [4 096].

I wanted to mention another place in which class size limit is mentioned, which is in section 1.2 of the Itanium C++ ABI draft

Various representations specified by this ABI impose limitations on conforming user programs. These include, for the 64-bit Itanium ABI:

The offset of a non-virtual base subobject in the full object containing it must be representable by a 56-bit signed integer (due to RTTI implementation). This implies a practical limit of 2**55 bytes on the size of a class.

Tags:

C++

Oop

Class