简体   繁体   English

课程的大小限制是多少?

[英]What is the size limit for a class?

I was wondering what the size limit for a class is. 我想知道课程的大小限制是多少。 I did a simple test: 我做了一个简单的测试:

#define CLS(name,other) \
class name\
{\
public: \
name() {};\
   other a;\
   other b;\
   other c;\
   other d;\
   other e;\
   other f;\
   other g;\
   other h;\
   other i;\
   other j;\
   other k;\
};

class A{
   int k;
public:
   A(){};
};

CLS(B,A);
CLS(C,B);
CLS(D,C);
CLS(E,D);
CLS(F,E);
CLS(G,F);
CLS(H,G);
CLS(I,H);
CLS(J,I);

It fails to compile with 它无法编译

"'J' : class is too large" “'J':上课太大了”

If I remove the final declaration - CLS(J,I); 如果我删除最后的声明 - CLS(J,I); , it all compiles fine. 这一切都很好。

Is this a compiler-imposed restriction, or is it somewhere in the standard? 这是编译器强加的限制,还是在标准的某个地方?

In C++11 this is Annex B. Implementations can impose limits, but they should be at least: 在C ++ 11中,这是附录B.实现可以施加限制,但它们至少应该是:

  • Size of an object [262 144]. 物体的大小[262 144]。
  • Data members in a single class [16 384]. 单个班级的数据成员[16 384]。
  • Members declared in a single class [4 096]. 成员在一个班级[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 . 您的实现似乎放弃了2 ^ 31个数据成员,或者大小为2 ^ 32,因为它接受了I而不是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. 编译器拒绝考虑大小大于SIZE_MAX类是非常明显的,即使程序没有实例化它或在类型上使用sizeof也是如此。 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. 因此,即使编译器尽可能地做出最大的努力,我也不会期望这可以在32位实现上工作。

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 ++标准中有各种机会使符合要求的实现变得无用,因此如果这是另一个,则不会造成额外的伤害。

C++03 is more-or-less the same: C ++ 03或多或少相同:

  • Size of an object [262 144]. 物体的大小[262 144]。
  • Data members in a single class, structure, or union [16 384]. 单个类,结构或联合中的数据成员[16 384]。
  • Members declared in a single class [4 096]. 成员在一个班级[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 我想提一下提到类大小限制的另一个地方,这是在Itanium C ++ ABI草案的1.2节中

Various representations specified by this ABI impose limitations on conforming user programs. 此ABI指定的各种表示对符合用户程序施加了限制。 These include, for the 64-bit Itanium ABI: 这些包括,对于64位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). 包含它的完整对象中的非虚拟基础子对象的偏移量必须由56位有符号整数表示(由于RTTI实现)。 This implies a practical limit of 2**55 bytes on the size of a class. 这意味着类的大小实际上限制为2 ** 55个字节。

I'm sure its compiler dependent. 我确定它的编译器依赖。 You can run your compiler in a preprocess only mode to see what the generated output is if you're curious. 您可以在仅预处理模式下运行编译器,以查看如果您感到好奇,生成的输出是什么。 You might also want to look at template expansion rather than macros. 您可能还想查看模板扩展而不是宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM