- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
假设我有两个具有相同初始成员序列的 POD 结构 A
和 B
,但后来有些不同(我知道,这可以通过继承轻松解决) .
struct A {
int x;
uint64_t y;
int z;
};
struct B {
int x;
uint64_t y;
int8_t z;
};
指向结构的指针需要指向初始成员(§9.2.20 [class.mem])。
现在还有两个问题:
两个结构中公共(public)成员的对齐方式必须相同吗?即,以下是否始终为真:
A a;
B b;
assert(offsetof(A, y) == offsetof(B, y));
如果不是:至少如果我将结构放入 union 中,那么这应该成立,因为标准(§9.2.19 [class.mem])说
If a standard-layout union contains two or more standard-layout structs that share a common initial sequence, and if the standard-layout union object currently contains one of these standard-layout structs, it is permitted to inspect the common initial part of any of them.
我在这里添加一个额外的例子。
struct A {
int x;
uint64_t y;
int z;
};
struct B {
int x;
uint64_t y;
int8_t z;
};
B convertToB(A& a);
void g() {
A a;
// at this point, I cannot rely on offset(A, y) == offset(B, y)
B b = convertToB(a);
// since I do the copy over the union, offset(A, y) == offset(B, y) holds
}
union U {
A asA;
B asB;
};
B convertToB(A& a) {
U u;
// at this point, offset(A, y) == offset(B, y) holds
u.asA = a;
return u.asB;
}
因为我没有机会知道是否在某个时候, union 发生了复制,并且结构必须在任何地方都相同,所以我声称,offset(A, y) == offset(B, y)
必须持有。
最佳答案
您可以在此处找到对象成员的排序要求:
9.2/13: Nonstatic data members of a (non-union) class with the same access control are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified. Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions and virtual base classes.
在您的 struct
中,所有成员都具有相同的访问类,因此您在这里可以保证顺序。
但请注意引用的最后一句话:不保证两个连续成员之间的空间相同(尽管可能性不大)。这就是为什么我建议采用 union
方法。您不仅可以从标准的保证中受益,而且还可以在源代码中清楚地传达您的意图。
编辑
我们还可以考虑另一个方面:POD 类型是一种普通类型,具有标准布局。
9.2/16: Two standard-layout struct types are layout-compatible if they have the same number of non-static data members and corresponding non-static data members (in declaration order) have layout-compatible types
3.9/11: If two types T1 and T2 are the same type, then T1 and T2 are layout-compatible types.
再一次,你发现你没有任何保证,只要你的结构在某个点发散。但是我知道这可以保证 u.asB
的布局与它返回的 B
对象兼容。
最后,我希望能找到一个更具体的可复制对象的保证:
3.9/2: For any object (other than a base-class subobject) of trivially copyable type T, whether or not the object holds a valid value of type T, the underlying bytes making up the object can be copied into an array of char or unsigned char. If the content of the array of char or unsigned char is copied back into the object, the object shall subsequently hold its original value.
我认为可以从中推断出,在您的情况下,将一个值存储在 union 中但读取另一个值是可行的。但是请注意,根据此(非规范)注释,这不是一般事实:
9.5/4: [Note: In general, one must use explicit destructor calls and placement new operators to change the active member of a union. —end note]
关于c++ - 普通旧数据 - 对齐要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37373006/
我有一个类和构造函数,如下所示: def init(log, edge): if edge: return Helper(log, edge) return Booka
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
有人知道在 mongo 上安装标准 ubuntu 需要多少磁盘空间和内存吗?试图找出我的 VPS 需求... 最佳答案 没有最低要求,但我不建议在与您的网络服务器相同的机器上运行 Mongo。 Mon
我的 Django 项目有一个虚拟环境,但是当我 pip 击 pip freeze 时,我得到了一个必须是全局站 pip 包列表的东西,包括太多包,比如ubuntu包和这么多不相关的东西。无论 vir
我曾尝试在 Heroku 上部署我的应用程序,但 smth 出错了。 错误:找不到满足要求的版本 get==2019.4.13(来自 -r/tmp/build_53ad6d03_/requiremen
我无法将 semantic-ui-calendar npm 模块加载到我的应用程序中。 我已经使用脚本标签成功地将它加载到我的 HTML 中, 但每次我尝试将它加载到我的应用程序中时,我都会出错。 在
如何修复 php.ini 中“require”函数内的地址?它进行故障排除并显示错误: 警告:require (..) 无法打开流:没有这样的文件或目录。 文件“db_connection.php”工
我有一个在 Node.js 应用程序中使用的外部库 ( Objection.js )。我创建了一个基本模型类,它为我的实体模型扩展了 Objection 的 Model 类: const { Mode
有谁知道在哪里可以找到RHEL5的GLIBC2.7,如果没有这个,Android模拟器将无法启动。它会给出一条消息,要求GLIBC 2.7或更高版本。 我尝试在网上搜索,但没有找到 最佳答案 我也遇到
Android 设备是否有任何要求/指南?例如按钮数量或所需的最少按钮数量。 还有没有菜单和后退按钮的安卓设备吗? (我知道就可用性而言,没有菜单/后退按钮会杀死大多数应用程序,我只是想了解更多有关该
我想要求/包含一个文件并将其内容检索到一个变量中。 test.php index.php ".$test; ?> 类似于 file_get_contents() 但它仍应执行 PHP 代码。这可能吗
我想要求/包含一个文件并将其内容检索到一个变量中。 test.php index.php ".$test; ?> 类似于 file_get_contents() 但它仍应执行 PHP 代码。这可能吗
我正在尝试在我的 Linux Mint 发行版上安装一个 python 模块“pyAudioProcessing”(https://github.com/jsingh811/pyAudioProces
我已经创建了我的第一个 composer 包,它具有 MySQL 和 MongoDB 的功能,但是,它不需要两者。我意识到有人可能只想将这个包与两个数据库之一一起使用,目前我有: "require":
我想调试以下函数,但假设在调试器中查看 moreajaj 的参数等于什么(假设不像在这个人为的示例中那么明显)是有用的。我可以在调试器框架中打印它,但是在每个参数的每个框架中都这样做很烦人。在宣布每一
我有一些生成的 GNUmakefiles,我需要从中提取变量的值。 有没有一种简单的方法可以在不修改 makefile 的情况下查看变量的值? 仅供引用,变量包含 emacs c-macro-expa
我正在使用 aspell 在 Linux 上拼写检查 LaTeX 文档。我的文档经常包含各种编程语言的代码示例,我希望 aspell 在拼写检查时简单地跳过这些行。 我可以在文档中写些什么来关闭一段文
我有一个包含多个列的数据集... 一列是具有重复值的主石斑鱼列,另一列是具有 bool 值 (1,0) 的 NUMBER,如下所示: grp bool --- ---- A 1 A 1 A
出于测试目的,我正在尝试删除一些 amd 模块并从服务器重新加载更新版本 - 目的是不刷新浏览器。 我目前正在执行以下操作,但浏览器仍然没有从网络重新加载项目。 var scripts = docum
当我键入irb> require 'rubygems'时,它返回false。我的Rails应用程序中有很多 gem ,这些 gem 显然可以正常工作-耙子,activerecord等。这里可能出什么问
我是一名优秀的程序员,十分优秀!