仅包含编译时常量的类的大小

发布于 2024-09-08 17:03:21 字数 362 浏览 6 评论 0原文

例如,如果我有一个仅包含编译时常量的类,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小…楫夜泊 2024-09-15 17:03:21

这些变量是否 const 并不重要;它们是静态,因此它们无论如何都不会影响类的大小。

sizeof(A) 不能为零,因此如果您创建 A 的实例,它的大小必须至少为 1 个字节。但是,将 A 作为基类并不一定会增加派生类的大小,因为“基类子对象的大小可能为零”(C++03 §1.8/5)。

It doesn't matter whether those variables are const; they are static, so they won't affect the size of the class anyway.

sizeof(A) cannot be zero, so if you create an instance of A it has to be at least one byte in size. However, having A 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).

最美不过初阳 2024-09-15 17:03:21

使用的空间

否,静态 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.

活泼老夫 2024-09-15 17:03:21

我相信每种类型的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:

When applied to a class, the result is the number of bytes in an object of that class including any padding required for placing objects of that type in an array. The size of a most derived class shall be greater than zero.

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.

温柔女人霸气范 2024-09-15 17:03:21

它被赋予了一个大小,只是因为即使是此类的对象也需要有一个大小。 (对于初学者来说,如果两个这样的对象是另一个对象的成员,如果您形成指向它们的成员指针,它们是否会有不同的地址?)当此类用作基类时,它将受益于 empty基类优化并减小到大小 0。事实上,这是

#include <iostream>

struct A {};
struct B { char x; };
struct C : public A, public B {};

int main()
{
    std::cout << sizeof(A) << '\n';
    std::cout << sizeof(B) << '\n';
    std::cout << sizeof(C) << '\n';
    return 0;
}

为我打印

1
1
1

的,因此 A 不会对 CC,这似乎支持我的解释,即 A1 大小是人为的。

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

#include <iostream>

struct A {};
struct B { char x; };
struct C : public A, public B {};

int main()
{
    std::cout << sizeof(A) << '\n';
    std::cout << sizeof(B) << '\n';
    std::cout << sizeof(C) << '\n';
    return 0;
}

prints

1
1
1

for me, so A doesn't contribute anything to the size of C, which seems to support my interpretation that A's size of 1 is artificial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文