仅包含编译时常量的类的大小
例如,如果我有一个仅包含编译时常量的类,
class A {
static const int x = 1;
static const int y = 2;
static const int z = 3;
};
我相信只要不获取常量的地址,它们就可以(将?)在使用它们的编译时被替换并且不会占用可执行文件中的任何空间(作为常量,也就是说,显然数字本身必须显示)。如果是这种情况,该类是否也可以被优化?而且,如果某些东西继承自类 A,但仍然只使用常量本身而不获取它们的地址,这种情况会改变吗?
哦,假设在非继承版本中,除了作为访问常量的手段之外,该类实际上并未在任何地方使用本身。
谢谢。
If I have a class that contains only compile-time constants, for example,
class A {
static const int x = 1;
static const int y = 2;
static const int z = 3;
};
I believe it's the case that, so long as the address of the constants is not taken, they can (will?) be replaced at compile time where they are used and will not take up any space in the executable (as constants that is, obviously the numbers themselves are going to have to show up). If this is the case can/will the class also be optimized out? And, will this change if something inherits from class A
, but still only uses the constants themselves and does not take their addresses?
Oh, and assuming, in the non-inheritance version, that the class is not actually used itself anywhere apart from as a means to access the constants.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这些变量是否 const 并不重要;它们是
静态
,因此它们无论如何都不会影响类的大小。sizeof(A)
不能为零,因此如果您创建A
的实例,它的大小必须至少为 1 个字节。但是,将A
作为基类并不一定会增加派生类的大小,因为“基类子对象的大小可能为零”(C++03 §1.8/5)。It doesn't matter whether those variables are
const
; they arestatic
, so they won't affect the size of the class anyway.sizeof(A)
cannot be zero, so if you create an instance ofA
it has to be at least one byte in size. However, havingA
as a base class does not necessarily increase the size of a derived class because "base class sub-objects may have zero size" (C++03 §1.8/5).使用的空间
否,静态 const int 成员将不会为其分配任何空间,因为它们被评估为编译时常量。
至于类对象的大小(即
sizeof(A)
),除非您正在创建类 A 的实例(您明确表示不是),否则这并不相关。使用命名空间来代替?
也就是说,也许您可以使用命名空间来使您的意图更清晰一些?除非您将它用于模板特征之类的用途,否则您似乎正在滥用class来完成名称空间的预期用途。
Space used
No, the static const int member will will not have any space allocated for them, as they are evaluated as compile time constants.
As for size of the class object (i.e.
sizeof(A)
), this is not relevant unless you are creating instances of the class A - which you explicitly said you are not.Use namespace instead?
That said, perhaps you could use namespace instead to make your intention a bit clearer? Unless you are using it for something like template traits, it seems you are abusing class to do the job namespaces are intended for.
我相信每种类型的
sizeof
都必须至少为1(部分是为了确保该类型的实例在数组中获得不同的地址)。C++0x 草案标准的第 5.3.3 节对此进行了规定:
它们不应该影响实例的大小(因为它们是静态的),但它们可能需要存储在可执行文件中的某个位置,因为您需要能够使用地址运算符他们。它们是否可以被优化而消失完全取决于编译器以及它是否可以告诉它们没有被取消引用。在这种特殊情况下,枚举可能是更好的工具。
但是,应用程序中的三个整数不太可能导致问题。
I believe that every type must have a
sizeof
of at least 1 (partly to ensure instance of that type get different addresses in an array for example).Section 5.3.3 of the C++0x draft standard is where this is dictated:
They shouldn't affect the size of an instance (since they're static) but they will probably need to be stored in the executable somewhere since you need to be able to use the address-of operator on them. Whether they can be optimised out of existence depends entirely on the compiler and whether it can tell they're not being de-referenced. Enumerations are probably a better tool to use in this particular case.
However, three integers in an application is unlikely to cause a problem.
它被赋予了一个大小,只是因为即使是此类的对象也需要有一个大小。 (对于初学者来说,如果两个这样的对象是另一个对象的成员,如果您形成指向它们的成员指针,它们是否会有不同的地址?)当此类用作基类时,它将受益于 empty基类优化并减小到大小 0。事实上,这是
为我打印
的,因此
A
不会对C
C,这似乎支持我的解释,即A
的1
大小是人为的。It's been given a size just because even object of such a class need to have a size. (For starters, if two such objects were members of another object, would they have distinct addresses if you form member pointers to them?) When this class is used as a base class, it will benefit from the empty base class optimization and be reduced to size 0. In fact, this
prints
for me, so
A
doesn't contribute anything to the size ofC
, which seems to support my interpretation thatA
's size of1
is artificial.