@
amiwrong123 注意括号 然后假设每次都一致,这是前提条件。。。
来看标准吧,c++20 标准 6.3 节 13 小节
There can be more than one definition of a
... -inline function or variable ([dcl.inline]), ...
这是允许 inline 函数或变量有多个定义(即使同一个 TU )再看需要满足的要求
(13.8)
Each such definition shall consist of the same sequence of tokens, where the definition of a closure type is considered to consist of the sequence of tokens of the corresponding lambda-expression.
每个定义都需要有相同的 token 序列
(13.9)
In each such definition, corresponding names, looked up according to [basic.lookup], shall refer to the same entity, after overload resolution ([over.match]) and after matching of partial template specialization ([temp.over]), except that a name can refer to ...... same entity in all definitions of D.
每个定义所引用的符号在重载解析,模板偏特化之后除了某些特殊情况外都必须解析到相同的实体上
最后
If these definitions do not satisfy these requirements, then the program is ill-formed; a diagnostic is required only if the entity is attached to a named module and a prior definition is reachable at the point where a later definition occurs
如果这些条件没有满足要求,那么程序是不合法的,而也只有特殊情况(同一个模块且在不一致定义要在同一个单元)才要求编译器给出诊断(报告错误)
所以一个不要求诊断的错误的代码(甚至不属于 UB )给出任何行为都是符合预期的(链接期随意选择是结果的一部分而不是原因,写程序不是自然科学,不要用实验来推测原因)