此代码是否违反一个定义规则
Does this code violate One Definition Rule?
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
的每个定义应由相同的令牌序列[..](参考(组成
开发人员只是"运气好",因为链接器没有试图做任何导致此违规症状的滑稽动作。
这就是名称空间的作用。例如,如果类类型只在定义它的转换单元中使用,那么它可能是在匿名命名空间中定义的。
- 此代码是否违反一个定义规则
- 静态结构和一个定义规则
- 为什么转换函数声明不需要至少一个定义类型说明符
- 只有一个定义/声明时标头声明变量的多堆定义错误
- 一个定义规则 - 编译
- 为什么传递给函数 set::itrator 而不是 const_iterator 违反了一个定义规则?
- 内联函数和一个定义规则
- 如何在C 中应用ODR(一个定义规则)
- C++ 多个定义,即使只给出了一个定义
- std::integral_constant<T, v>::value 总是有一个定义吗?
- 为什么这不违反一个定义规则
- C 是纯虚拟函数,也是一个定义或仅声明
- 从标准库重新定义函数是否违反了一个定义规则
- 是否使用“ __Date__”或“ __Time__”违反了一个定义规则
- 非静态数据成员和一个定义规则
- 如何在C++中声明一个定义为func(cv::Mat &img)的函数
- 现代C和C :可以将一个定义的结构用于其他声明的结构
- C 是平行阵列一个定义的结构
- 当一个定义位于命名空间中时不明确的函数调用
- C++:同名的文件范围常量违反了一个定义规则