此代码是否违反一个定义规则

Does this code violate One Definition Rule?

本文关键字:一个 定义 规则 代码 是否      更新时间:2023-10-16

AOSP10中的某些代码似乎违反了ODR:

来源1:

struct ExtentsParam
{
void init (const OT::cff1::accelerator_t *_cff)
{
path_open = false;
cff = _cff;
bounds.init ();
}
void start_path ()         { path_open = true; }
void end_path ()           { path_open = false; }
bool is_path_open () const { return path_open; }
bool    path_open;
Bounds  bounds;
const OT::cff1::accelerator_t *cff;
};

发件人:https://android.googlesource.com/platform/external/harfbuzz_ng/+/refs/heads/android10 gsi/src/hb-ot-cff1-table.cc

来源2:

struct ExtentsParam
{
void init ()
{
path_open = false;
min_x.set_int (0x7FFFFFFF);
min_y.set_int (0x7FFFFFFF);
max_x.set_int (-0x80000000);
max_y.set_int (-0x80000000);
}
void start_path ()         { path_open = true; }
void end_path ()           { path_open = false; }
bool is_path_open () const { return path_open; }
void update_bounds (const Point &pt)
{
if (pt.x < min_x) min_x = pt.x;
if (pt.x > max_x) max_x = pt.x;
if (pt.y < min_y) min_y = pt.y;
if (pt.y > max_y) max_y = pt.y;
}
bool  path_open;
Number min_x;
Number min_y;
Number max_x;
Number max_y;
};

发件人:https://android.googlesource.com/platform/external/harfbuzz_ng/+/refs/heads/android10 gsi/src/hb-ot-cff2-table.cc

构建脚本:

...
srcs: [
...
"src/hb-ot-cff1-table.cc",
"src/hb-ot-cff2-table.cc",
],
...

https://android.googlesource.com/platform/external/harfbuzz_ng/+/参考文献/heads/android10 gsi/Android.bp

这些也被构建在同一个共享库中。两个源都有"structExtentsParam"的定义,内容完全不同。这两个结构似乎都只能在本地使用。

这两个来源的名字相似,因此非故意重复名字的可能性很低。谷歌违反ODR的可能性可能很低。

是吗?

:由于它们都在全局命名空间中,这绝对违反了ODR。

仅在定义类类型的翻译单元中使用的类类型没有豁免;一个程序只能包含一个具有任何给定名称的类类型。

它属于满足该规则豁免标准的第一个要求:

类类型[..]可以有多个定义在程序中,假设每个定义出现在不同的翻译单元中,并且假设定义满足以下要求[..]如果在多个翻译单元中定义了这样一个名为D的实体,则应满足以下所有要求[..]D的每个定义应由相同的令牌序列[..](参考(组成

开发人员只是"运气好",因为链接器没有试图做任何导致此违规症状的滑稽动作。

这就是名称空间的作用。例如,如果类类型只在定义它的转换单元中使用,那么它可能是在匿名命名空间中定义的。