C++包装类的大小
假设我有一个类 A,它不从任何东西继承,没有虚方法,并且只有一个类型为 T 的变量。C++ 是否保证 sizeof(A) == sizeof(T) ?
编辑:
另外,如果 T 是复合类型,会有什么不同吗?
Suppose I have a class A that does not inherit from anything, has no virtual methods, and has exactly one variable of type T. Does C++ guarantee sizeof(A) == sizeof(T)
?
EDIT:
Also if T were a composite type, would it make a difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,由于填充的原因,它可能会超过
sizeof(T)
。No, it might be more than
sizeof(T)
due to padding.我不认为它明确地保证了这一点,但我认为它不会有什么不同。
I don't think it explicitly guarantees it, but I don't think it would ever be different.
我认为C++应该保证sizeof(A) == sizeof(T)。
考虑以下情况,C++ 应该使其像 C 中一样工作:
I think C++ should guarantee sizeof(A) == sizeof(T).
Consider bellow situation, C++ should make it works just like in C: